我有桌子
account_no | name | date | amount
1101 | A | 2018-11-20 | 50
1101 | A | 2018-11-20 | 20
1102 | B | 2018-11-20 | 30
1101 | A | 2018-11-19 | 100
1101 | A | 2018-11-18 | 80
1102 | B | 2018-11-19 | 70
我想要下面的输出
account_no | name | group(today) | group(month)
1101 | A | 70 | 250
1102 | B | 30 | 100
无论是我今天的金额还是月份的金额,都不是一排排的。到目前为止我尝试过的
select account_no, sum(today) from account group by account_no having date = '<today>'
select account_no, sum(today) from account group by account_no having date between '<firstDay>' and '<today>'
我可以考虑使用UNION,除了提供重复的行。
发布于 2018-11-21 06:45:31
您可以尝试使用条件聚合。
select account_no,name, sum(case when yourdate='<today>' then amount end) as sumoftoday,
sum(amount) for sumofmonth
from tablename
where yourdate between '<firstDay>' and '<today>'
group by account_no,name
https://stackoverflow.com/questions/53406297
复制相似问题