gpt4 book ai didi

sql - 逐行求和并选择结果集中的其他列

转载 作者:行者123 更新时间:2023-12-05 04:15:20 24 4
gpt4 key购买 nike

我目前的查询如下:

select id, quantity, c, d
from table1;

这将给我示例数据如下:

id | quantity | c | d
---------------------
1 | 1 | x | y
1 | 3 | x | y
2 | 1 | x | y
2 | 1 | x | y

但是我想按 ID 分组并得到如下数量的总和:

id | quantity | c | d
---------------------
1 | 4 | x | y
2 | 2 | x | y

我尝试修改我的第一个查询,以在 id 上包含一个分组依据,在数量上包含一个总和:

select id, sum(quantity), c, d
from table1
group by id;

但是我得到了一个错误,因为其他 2 列不是 group by 子句的一部分。如何包含它们?

最佳答案

简单地将它们添加到组中:

select id, sum(quantity), c, d from table1 group by id, c, d;

关于sql - 逐行求和并选择结果集中的其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com