请教一个聚合查询的SQL,请高手指教

haitr 2012-05-23 10:01:13


说明:
InOrOut,收入(1)/支出(0)
Account, 储蓄账户ID
Money,消费/收入金额
PYDate,记录时间


需求:

查询每个账户的最后消费日期,总共收入和支出金额

还望能得到高手的帮助啊。。 谢谢了
...全文
148 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

SQL code
select Account,max(PYDate) as 最后消费日期,
sum(case when InOrOut=1 then [money] else 0 end) as 总共收入,
sum(case when InOrOut=0 then [money] else 0 end) as 支出金额
from tb
group by Account
[/Quote]

看不到图片,是树哥这意思么?
百年树人 2012-05-23
  • 打赏
  • 举报
回复
select Account,max(PYDate) as 最后消费日期,
sum(case when InOrOut=1 then [money] else 0 end) as 总共收入,
sum(case when InOrOut=0 then [money] else 0 end) as 支出金额
from tb
group by Account

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧