Home How to make raw SQL query chainable using Scope? -Rails
Reply: 0

How to make raw SQL query chainable using Scope? -Rails

user5199
1#
user5199 Published in September 21, 2018, 2:39 am

I have written a SQL and i want this to be chainable or i can use this query as a scope in Rails? any thoughts?

select '{Critical: '||"Critical"||', Major: '||"Major"||', Moderate: 
'||"Moderate"||', Minor: '||"Minor"||', Clear: '||"Clear"||'}' as result
from (
    select
      sum(case when max_weight=1   THEN 1 else 0 End ) as "Clear",
      sum(case when max_weight=2   THEN 1 else 0 End ) as "Minor",
      sum(case when max_weight=3   THEN 1 else 0 End ) as "Moderate",
      sum(case when max_weight=22  THEN 1 else 0 End ) as "Major",
      sum(case when max_weight=160 THEN 1 else 0 End ) as "Critical",
      count(*) as number_of_answers
    from (
      select 
        fua.forms_user_id, 
        max(fa.weight) max_weight
      from 
        public.forms_user_answers fua
        join public.forms_users fu on (fua.forms_user_id  = fu.id)
        left join public.form_answers fa on (fua.form_answer_id = fa.id)
      where 
        fu.form_id = #{form.id}
      group by 
        fua.forms_user_id
    ) subq
) subq1
share|improve this question

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304004 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO