数据表:
业务日期 帐号 姓名 期初余额 存入 支出 利息 期末余额
2007年8月5日 1230 张三 0 10 0 0 10
2008年1月2日 1230 张三 10 5 0 0 15
2008年1月5日 1230 张三 15 3 0 0 18
2005年5月1日 1231 李四 0 55 0 0 55
2008年3月2日 1231 李四 55 0 5 0 50要查询2008年每个帐号年初的期初余额、每月的合计存款,全年存款、利息、支出、期末余额,按帐号增序排列。格式如下:序号 帐号 姓名 期初余额 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 全年 利息 支出 期末余额
1 1230 张三 10 8 0 0 0 0 0 0 0 0 0 0 0 8 0 0 18
2 1231 李四 55 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 50 困扰我许久了,请教各位老师!
业务日期 帐号 姓名 期初余额 存入 支出 利息 期末余额
2007年8月5日 1230 张三 0 10 0 0 10
2008年1月2日 1230 张三 10 5 0 0 15
2008年1月5日 1230 张三 15 3 0 0 18
2005年5月1日 1231 李四 0 55 0 0 55
2008年3月2日 1231 李四 55 0 5 0 50要查询2008年每个帐号年初的期初余额、每月的合计存款,全年存款、利息、支出、期末余额,按帐号增序排列。格式如下:序号 帐号 姓名 期初余额 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 全年 利息 支出 期末余额
1 1230 张三 10 8 0 0 0 0 0 0 0 0 0 0 0 8 0 0 18
2 1231 李四 55 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 50 困扰我许久了,请教各位老师!
问
数据表Mytable
WorkID Name SaleDate DayCount
001 张三 2008-01-01 3
002 李四 2008-01-10 5
001 张三 2008-01-20 1
003 老王 2008-01-20 2
001 张三 2008-04-01 6
002 李四 2008-04-02 7
004 小强 2008-04-03 8
要求按月输出明细:
2008-01明细:
WorkID Name 2008-01-01 2008-01-02 2008-01-03 2008-01-04 ...... 2008-01-31
001 张三 3 0 0 0 0
002 李四 0 0 0 0 0
003 老王 0 0 0 0 0
004 小强 0 0 0 0 0 答:创建存储过程,动态生成SQL即可create proc p_test @date varchar(10)
as
declare @sql varchar(8000)
declare @i int select @sql='select WorkID,Name',@i=1
while 1=1
begin
select @sql=@sql+',sum(case when convert(char(10),SaleDate,120)='''+(@date+'-'+right('00'+ltrim(@i),2))+''' then DayCount else 0 end) ['+(@date+'-'+right('00'+ltrim(@i),2))+']',
@i=@i+1
if @i>day(dateadd(day,-1,dateadd(month,1,@date+'-1'))) break
end
exec (@sql+' from tb group by WorkID,Name order by WorkID')
go
exec p_test '2008-04'
insert into tb select 1,'2008-01-01',10
insert into tb select 2,'2008-01-01',10
insert into tb select 3,'2008-02-01',20
insert into tb select 4,'2008-02-01',20
insert into tb select 5,'2008-03-01',30
insert into tb select 6,'2008-03-01',30
insert into tb select 7,'2008-04-01',40
insert into tb select 8,'2008-04-01',40
insert into tb select 9,'2008-05-01',50
insert into tb select 10,'2008-05-01',50
insert into tb select 11,'2008-06-01',60
insert into tb select 12,'2008-06-01',60
insert into tb select 13,'2008-07-01',70
insert into tb select 14,'2008-07-01',70
insert into tb select 15,'2008-08-01',80
insert into tb select 16,'2008-08-01',80
insert into tb select 17,'2008-09-01',90
insert into tb select 18,'2008-09-01',90
insert into tb select 19,'2008-10-01',100
insert into tb select 20,'2008-10-01',100
insert into tb select 21,'2008-11-01',110
insert into tb select 22,'2008-11-01',110
insert into tb select 23,'2008-12-01',120
insert into tb select 24,'2008-12-01',120--静态查询
select
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='01' then sale else 0 end) as '1月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='02' then sale else 0 end) as '2月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='03' then sale else 0 end) as '3月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='04' then sale else 0 end) as '4月',sum(case when datepart(yy,date)='2008' and datepart(mm,date)='05' then sale else 0 end) as '5月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='06' then sale else 0 end) as '6月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='07' then sale else 0 end) as '7月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='08' then sale else 0 end) as '8月',sum(case when datepart(yy,date)='2008' and datepart(mm,date)='09' then sale else 0 end) as '9月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='10' then sale else 0 end) as '10月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='11' then sale else 0 end) as '11月',
sum(case when datepart(yy,date)='2008' and datepart(mm,date)='12' then sale else 0 end) as '12月'
from tb
--插入2009年数据
insert into tb select 25,'2009-01-01',10
--动态查询
declare @sql varchar(max)
select @sql=isnull(@sql+',','')+'sum(case when datepart(yy,date)='''+ltrim([year])+''' and datepart(mm,date)='''+ltrim([month])+''' then sale else 0 end) as ['+ltrim([year])+'年'+ltrim([month])+'月]'
from (select distinct datepart(yy,date) as [year],datepart(mm,date) as [month] from tb)a
exec('select '+@sql+' from tb')
Set Nocount On
if not object_id('Test') is null
drop table Test
Go
Create table Test([业务日期] Datetime,[帐号] int,[姓名] nvarchar(2),[期初余额] nvarchar(2),[存入] money,[支出] money,[利息] money,[期末余额] money)
Insert Test
select '2007/8/5',1230,N'张三',N'0',N'10',N'0',N'0',10 union all
select '2008/1/2',1230,N'张三',N'10',N'5',N'0',N'0',15 union all
select '2008/1/5',1230,N'张三',N'15',N'3',N'0',N'0',18 union all
select '2005/5/1',1231,N'李四',N'0',N'55',N'0',N'0',55 union all
select '2008/3/2',1231,N'李四',N'55',N'0',N'5',N'0',50
Go
Declare @Year int
Set @Year=2008
;With t1 As
(
Select [帐号],[姓名],[期初余额] from Test a where Year([业务日期])=@Year
And Not Exists(Select 1 From Test Where Year([业务日期])=@Year And a.[帐号]=[帐号] And [业务日期]<a.[业务日期])
)
,t2 As (Select [帐号],[全年存款]=Sum([存入]),[利息]=Sum([利息]),[支出]=Sum([支出]) From Test where Year([业务日期])=@Year Group By [帐号])
,t3 As
(
Select * From
(Select [帐号],[月份]=Month([业务日期]),[存入] From Test) a
Pivot
(Sum([存入]) For [月份] In([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) b
)
Select a.*,
[1月]=Isnull(c.[1],0),
[2月]=Isnull(c.[2],0),
[3月]=Isnull(c.[3],0),
[4月]=Isnull(c.[4],0),
[5月]=Isnull(c.[5],0),
[6月]=Isnull(c.[6],0),
[7月]=Isnull(c.[7],0),
[8月]=Isnull(c.[8],0),
[9月]=Isnull(c.[9],0),
[10月]=Isnull(c.[10],0),
[11月]=Isnull(c.[11],0),
[12月]=Isnull(c.[12],0),
b.[全年存款],
b.[利息],
b.[支出],
[期末余额]=a.[期初余额]+b.[全年存款]+b.[利息]-b.[支出]
From t1 a,t2 b,t3 c
Where a.[帐号]=b.[帐号] And b.[帐号]=c.[帐号]/*
帐号 姓名期初余额 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 全年存款 利息 支出 期末余额
----------------------------------------------------------------------------------------------------------------------------------------------------------
1230 张三 10 8.00 0.00 0.00 0.00 0.00 0.00 0.00 10.00 0.00 0.00 0.00 0.00 8.00 0.00 0.00 18.00
1231 李四 55 0.00 0.00 0.00 0.00 55.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.00 50.00
*/
是什么意思呢?是创建了一个查询表或是说是记录集吗?
2、Select [帐号],[姓名],[期初余额] from Test a where Year([业务日期])=@Year
其中的 a 不明白,是Test 的别名吗?
3、Select * From (Select [帐号],[月份]=Month([业务日期]),[存入] From Test) a
Pivot (Sum([存入]) For [月份] In([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) b
其中的a和b又是它们前方语句的别名吗?
4、Select a.*, [1月]=Isnull(c.[1],0), ...
其中的c是何意?前面没有出现过这个东东的;顺便请教Isnull(c.[1],0)的Isnull
5、From t1 a,t2 b,t3 c ...
一个逗号包含两个名,好像还有附属关系,也希望说明一下!
6、开帖的时候好像本题20分,如何给别人加上?自己暂时没分也能给别人加吗?以上一堆问题,有劳各位高手援手,深深感谢!
既然是表就理解了,编程语言虽然有所差异,但SQL语句的差异很小,所以只要把SQL中的意思弄明白了,我就能测试了,继续请高手赐教,感谢!