Rails计算多列的总和avg,而不是group by

时间:2014-08-13 02:32:56

标签: ruby-on-rails ruby

我是Rails的新手。

我知道多列上的'group'是可以的。但我不希望这样,我想在多列上计数(或总和,或平均)。

我知道:

issues = Issue.group(:tracker_id).count(:subject)

有查询:

SELECT COUNT(`issues`.`subject`) AS count_subject, tracker_id AS tracker_id FROM `issues` GROUP BY tracker_id

但它只计算一个专栏'主题'。我尝试在下面添加一个列(status_id)作为2个语句,但它们不起作用:

1. issues = Issue.group(:tracker_id).count(:subject).count(:status_id)

2. issues = Issue.group(:tracker_id).count(:subject, :status_id)

我该怎么做?谢谢。

1 个答案:

答案 0 :(得分:0)

感谢: group by + sum on multiple columns in rails 3

我可以使用一些SQL:

issues = Issue.group(:tracker_id)
        .select([:tracker_id, "COUNT(subject)", "COUNT(status_id)"])

不知道这是最好的吗?