【发布时间】:2018-10-17 05:16:53
【问题描述】:
我很难理解 Postgesql 和按关联分组。
我有一个重复的试验。我想按列重复分组以在变量中使用。目前我有:
@trials = Trial.joins(:repetitions).group('repetitions.repetition_index')
但是得到这个错误。
PG::GroupingError: ERROR: column "trials.id" must appear in the GROUP BY clause or be used in an aggregate function
现在通过阅读有关此内容,我需要在我的组方法中包含trials.id,但是当我这样做时,输出不再按repetitions.repetition_index 分组。似乎它按trials.id 分组。
如何确保该组仅由 repetitions.repetition_index 提供?
更新
尝试:
@trials = Trial.joins(:repetitions).select('repetitions.repetition_index,repetitions.treatment_index').group('trials.id, repetitions.repetition_index,repetitions.treatment_index')
然后调用:
<% @trials.each do |r| %>
<table class="table table-bordered">
<tr>
<td><%= r.repetition_index %></td>
<td><%= r.treatment_index %></td>
</tr>
</table>
<% end %>
给我一个输出:
|1|1|
-----
|1|2|
-----
|2|1|
-----
|2|2|
-----
当我想得到:
| |1|
|1|2|
-----
| |1|
|2|2|
-----
【问题讨论】:
标签: ruby-on-rails postgresql ruby-on-rails-5