gpt4 book ai didi

sql - 仅对具有多行的组求和值

转载 作者:行者123 更新时间:2023-11-29 13:46:22 25 4
gpt4 key购买 nike

我有这个问题:

SELECT extract(year from date1), extract(month from date1), spending    
FROM ( SELECT *, COUNT(*) OVER(PARTITION BY CONCAT(extract(year FROM date1), extract(month FROM date1))) N
FROM table) as A
WHERE N > 1
GROUP BY date1
ORDER BY date1 ASC;

结果是:

enter image description here

仅当有不止一行包含 equla yearmonth 时,我才需要对字段 spending 求和。期望的结果:

year    month   spending
---- ----- --------
2015 1 5424
2016 1 605886
2016 5 xxxxxx
.... .. ......

最佳答案

好的,我找到了解决方案:拥有:

SELECT extract(year from date1), extract(month from date1), spending    
FROM table
GROUP BY extract(month from date1)), extract(year from date1), extract(month from date1)
HAVING count (CONCAT(extract(year from date1), extract(month from date1))) > 1
ORDER BY extract(year from date1), extract(month from date1) ASC;

以防对某人有帮助。

关于sql - 仅对具有多行的组求和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47791862/

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