首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对具有多个条件的多列使用Group查询

对具有多个条件的多列使用Group查询
EN

Stack Overflow用户
提问于 2018-11-21 06:21:27
回答 1查看 94关注 0票数 1

我有桌子

代码语言:javascript
运行
复制
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

我想要下面的输出

代码语言:javascript
运行
复制
account_no | name  | group(today) | group(month)
1101       |   A   |     70       |  250 
1102       |   B   |     30       |  100  

无论是我今天的金额还是月份的金额,都不是一排排的。到目前为止我尝试过的

代码语言:javascript
运行
复制
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,除了提供重复的行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-21 06:45:31

您可以尝试使用条件聚合。

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53406297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档