Re: Not able to understand how to write group by

From: Arup Rakshit <aruprakshit(at)rocketmail(dot)com>
To: David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Not able to understand how to write group by
Date: 2014-07-03 11:01:35
Message-ID: 1404385295.4791.YahooMailNeo@web193903.mail.sg3.yahoo.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Without commenting on the rest of it...to combine what you show here just
GROUP BY gender and SUM() everything else (i.e., turn the above into a
subquery and then do this)

David J.

Exactly.. I am done. Here is the ORM query :-

  def self.employee_learning_by_gender(question_id)
    cpd_id = Measure.find_by(option: 'CPD').id
    other_id = Measure.find_by(option: 'Others').id
   
    User.select("view.gender, sum(view.participant) as participant, sum(cpd) as cpd, sum(other) as other").from(User.joins(daily_action_answers: [:measure]).group("users.gender, daily_action_answers.measure_id")
                                                .where("((daily_action_answers.day between ? and ?) and
                                                         daily_action_answers.daily_action_id = ?)",
                                                         Date.today, Date.today.end_of_year,
                                                         question_id
                                                       )
                                                .select("users.gender, count(*) as participant,
                                                         case when daily_action_answers.measure_id = #{cpd_id} then avg(daily_action_answers.value) end as cpd,
                                                         case when daily_action_answers.measure_id = #{other_id} then avg(daily_action_answers.value) end as other"
                                                       ), :view).group("view.gender")
  end

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Augori 2014-07-03 11:30:04 Failed to build transaction for opengeo-server
Previous Message Orestis Tsakiridis 2014-07-03 10:25:38 postgresql server version & JDBC driver version