多个 GROUP BY 并根据第一个 GROUP BY 的结果选择值
Posted
技术标签:
【中文标题】多个 GROUP BY 并根据第一个 GROUP BY 的结果选择值【英文标题】:Multiple GROUP BY and select value dependent on result of the first GROUP BY 【发布时间】:2014-11-22 22:34:39 【问题描述】:我得到以下数据库表:
combination_id | weight | group | std
-------------------------------------------------
1 | 50 | 3 | 7
2 | 30 | 3 | 19
3 | 30 | 3 | 19
4 | 25 | 4 | 7
我按 group 和 std 列对条目进行分组,并总结 weight 列的值:
SELECT SUM(weight) as weight_sum, group, std FROM weight_table
WHERE combination_id IN (1, 2, 3)
GROUP BY group, std
ORDER BY weight_sum DESC;
结果如下:
weight | group | std
-----------------------------------------------
60 | 3 | 19
50 | 3 | 7
25 | 4 | 7
现在我想做第二个 GROUP BY,但只在 group 列上,并在 weight 列上求和。结果中std列的值应该是上次查询中权重最高且同组的条目的std列的值。 所以对于 3 组,我希望为 std 选择 19,因为 60 是最高的 3 组的权重:
weight | group | std
-----------------------------------------------
110 | 3 | 19
25 | 4 | 7
我怎样才能做到这一点? 我正在使用 sqlite 3。
【问题讨论】:
【参考方案1】:我想你想要这个:
SELECT SUM(weight) as weight_sum, group, max(std) as std FROM weight_table
WHERE combination_id IN (1, 2, 3)
GROUP BY group
ORDER BY weight_sum DESC;
换句话说,不要认为这是“多个分组”。将其视为单个聚合,您可以在其中获得权重总和以及 std
的最大值。
编辑:
我似乎误解了这个问题。这在 SQL lite 中有点痛苦。这是一种方法:
with w as (
SELECT SUM(weight) as weight_sum, group, std
FROM weight_table
WHERE combination_id IN (1, 2, 3)
GROUP BY group, std
),
wmax as (
SELECT group, MAX(weight_sum) as maxws
FROM w
GROUP BY gruop
)
select w.group, sum(w.weight_sum) as weight_sum,
max(case when w.weight_sum = wmax.weight_sum then std end) as std
from w left join
wmax
on w.group = wmax.group
group by w.group
order by weight_sum DESC;
【讨论】:
他想要std
基于最大weight_sum
,这使它成为greatest-n-per-group,而不是简单的聚合。
@Clockwork-Muse 。 . .谢谢你。我确定了答案。以上是关于多个 GROUP BY 并根据第一个 GROUP BY 的结果选择值的主要内容,如果未能解决你的问题,请参考以下文章
r - 使用 group_by 和 mutate 根据多个条件添加新列时出现意外的“=”