求一SQL查询语句
直接写题目要求了:
SQL Server2000数据库.
表结构如下:
fdYear fdMonth fdAmount fdStatus
2007 01 2000 0
2007 02 3000 1
2007 02 1000 0
2007 03 2000 1
2007 01 3000 0
...
结果:指定年度下按月列出fdAmount总数量,fdStatus为0的数量
结果如下形式:
fdYear fdMonth fdAmount fdAmount1
2007 01 5000 5000
2007 02 4000 1000
2007 03 2000 0
...
先谢谢了!
------解决方案--------------------Select
fdYear,
fdMonth,
SUM(fdAmoun) As fdAmount,
SUM(Case fdStatus When 0 Then fdAmoun Else 0 End) As fdAmount1
From
TableName
Group By fdYear, fdMonth
------解决方案--------------------create table A(fdYear int, fdMonth varchar(3), fdAmount int, fdStatus int)
insert into a select 2007, '01 ', 2000, 0
union all select 2007, '02 ', 3000, 1
union all select 2007, '02 ', 1000, 0
union all select 2007, '03 ', 2000, 1
union all select 2007, '01 ', 3000, 0
select fdYear,fdMonth,sum(fdAmount),sum(case when fdStatus=1 then fdAmount else 0 end)
from a group by fdYear,fdMonth
------解决方案--------------------来个麻烦的
select t1.*,(select sum(fdamount) from A where A.fdyear=t1.fedyear and A.fdmonth=t1.fdmonth and A.fdstatus=0) as fdamount1 from (select fdyear,fdmonth,sum(fdamount) as fdamount from A where fdstatus=0 group by fdyear,fdmonth) t1;