我有这个问题:
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;
结果是:
仅当有不止一行包含 equla year
和 month
时,我才需要对字段 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/