SQL语句难题,高手进来!!

cbzdream 2003-11-05 10:52:04
有表Temp:

amount PayDate Bond Direction Name
100 2003-11-03 0001 1 aaa
200 2003-11-03 0001 -1 aaa
300 2003-11-04 0001 1 aaa
400 2003-11-03 0002 1 aaa
800 2003-11-03 0002 -1 aaa

要得出:
amount1 amount2 PayDate Bond Name
100 200 2003-11-03 0001 aaa
400 800 2003-11-03 0002 aaa
300 0 2003-11-04 0001 aaa

其中amount1代表Direction=1的amount的值,
amount2代表Direction=2的amount的值
在相同的PayDate下,如果Direction=1的amount的值不存在,则amount1=0,
如果Direction=-1的amount的值不存在,则amount2=0,
结果按Bond分组
请问SQL怎么写???谢谢!!!
...全文
36 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
银狐被占用 2003-11-05
  • 打赏
  • 举报
回复
select (case when Direction=1 then amount else 0 end) as amount1,
(case when Direction=-1 then amount else 0 end) as amount2,
PayDate,Bond,Name
from Temp
group by PayDate,Bond,Name
zjcxc 元老 2003-11-05
  • 打赏
  • 举报
回复
select
amount1=sum(case when Direction=1 then amount else 0 end),
amount2=sum(case when Direction=-1 then amount else 0 end)
,Bond,PayDate,name
from temp group by Bond,PayDate,name
pengdali 2003-11-05
  • 打赏
  • 举报
回复
select
sum(case when Direction=1 then amount else 0 end) amount1,
sum(case when Direction=-1 then amount else 0 end) amount2
,Bond,PayDate,name from temp group by Bond,PayDate,name

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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