Re: Not able to understand how to write group by

From: John R Pierce <pierce(at)hogranch(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Not able to understand how to write group by
Date: 2014-07-03 16:04:36
Message-ID: 53B57F14.3060805@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7/3/2014 4:01 AM, Arup Rakshit wrote:
> 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

OT, but it boggles my mind that anyone thinks thats 'better' than the
straight SQL

--
john r pierce 37N 122W
somewhere on the middle of the left coast

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message sunpeng 2014-07-03 17:02:14 How to use ADO to insert BYTEA data?
Previous Message Steve Crawford 2014-07-03 15:52:02 Re: NOT IN and NOT EXIST