求如下sql语句
数据表1:
类型,名称,数量,金额, 时间
a x1 5 700 2007-01-01
a x2 10 1400 2007-01-02
b x3 6 700 2007-01-02
c x4 9 600 2007-01-03
..........
表2:
金额, 时间
500 2007-01-01
400 2007-01-02
300 2007-01-02
400 2007-01-03
.............
要求生成表3
日期, a数量,a金额,b数,b金,c数,c金,本日合计,本日收款,本日结算
2007-01-01 5 700 0 0 0 0 700 500 200
2007-01-02 10 1400 6 700 0 0 2100 700 1400
2007-01-03 0 0 0 0 9 600 600 400 200
2007-01-04 0 0 0 0 0 0 0 0 0
........
[解决办法]
没测试过:
表ta:
类型,名称,数量,金额, 时间
a x1 5 700 2007-01-01
a x2 10 1400 2007-01-02
b x3 6 700 2007-01-02
c x4 9 600 2007-01-03
declare @s varchar(4000)
set @s= ' '
select @s=@s+ ', '+quotename(类型+ '数量 ')+ '=sum(case when 类型= '+quotename(类型, ' ' ' ')+
' then 数量 else 0 end), '+
quotename(类型+ '金额 ')+ '=sum(case when 类型= '+quotename(类型, ' ' ' ')+
' then 金额 else 0 end) '
from ta
group by 类型
set @s= 'select [日期]=convert(varchar(10),时间,120) '+@s+ ' from ta group by convert(varchar(10),时间,120) '
exec(@s)
[解决办法]
--测试表
create table 表1(类型 varchar(10),名称 varchar(10),数量 int,金额 int,时间 datetime)
create table 表2(金额 int,时间 datetime)
insert 表1 select 'a ', 'x1 ', 5, 700 , '2007-01-01 '
union all
select 'a ' , 'x2 ' , 10 , 1400 , '2007-01-02 '
union all
select 'b ' , 'x3 ' , 6 , 700 , '2007-01-02 '
union all
select 'c ' , 'x4 ' , 9 , 600 , '2007-01-03 '
union all
select 'd ', 'x1 ',5,500, '2007-01-04 '
insert 表2 select 500 , '2007-01-01 '
insert 表2 select 400 , '2007-01-02 '
insert 表2 select 300 , '2007-01-02 '
insert 表2 select 400 , '2007-01-03 '
--查询
declare @sql1 varchar(8000),@sql2 varchar(8000)
select @sql1= 'select 时间 '
select @sql1=@sql1+ ', '+类型+ '数量=sum(case when 类型= ' ' '+类型+ ' ' ' then 数量 else 0 end), '
+类型+ '金额=sum(case when 类型= ' ' '+ 类型+ ' ' ' then 金额 else 0 end) '
from (select distinct 类型 from 表1) a
select @sql2= ',本日合计=(select sum(金额) from 表1 where 时间=a.时间 group by 时间),
本日收款=isnull((select sum(金额) from 表2 where 时间=a.时间 group by 时间),0),
本日结算=(select sum(金额) from 表1 where 时间=a.时间 group by 时间)-
isnull((select sum(金额) from 表2 where 时间=a.时间 group by 时间),0)
from 表1 a group by 时间 '
exec(@sql1+@sql2)
--删除测试表
drop table 表1,表2
/*结果
时间 a数量 a金额 b数量 b金额 c数量 c金额 d数量 d金额 本日合计 本日收款 本日结算
----------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2007-01-01 00:00:00.000 5 700 0 0 0 0 0 0 700 500 200
2007-01-02 00:00:00.000 10 1400 6 700 0 0 0 0 2100 700 1400
2007-01-03 00:00:00.000 0 0 0 0 9 600 0 0 600 400 200
2007-01-04 00:00:00.000 0 0 0 0 0 0 5 500 500 0 500
*/
[解决办法]
if object_id( 'pubs..t1 ') is not null
drop table t1
go
create table t1(类型 varchar(10),名称 varchar(10),数量 int,金额 int,时间 varchar(10))
insert into t1(类型,名称,数量,金额,时间) values( 'a ', 'x1 ', 5, 700 , '2007-01-01 ')
insert into t1(类型,名称,数量,金额,时间) values( 'a ', 'x2 ', 10, 1400 , '2007-01-02 ')
insert into t1(类型,名称,数量,金额,时间) values( 'b ', 'x3 ', 6 , 700 , '2007-01-02 ')
insert into t1(类型,名称,数量,金额,时间) values( 'c ', 'x4 ', 9 , 600 , '2007-01-03 ')
go
if object_id( 'pubs..t2 ') is not null
drop table t2
go
create table t2(金额 int,时间 varchar(10))
insert into t2(金额,时间) values(500, '2007-01-01 ')
insert into t2(金额,时间) values(400, '2007-01-02 ')
insert into t2(金额,时间) values(300, '2007-01-02 ')
insert into t2(金额,时间) values(400, '2007-01-03 ')
go
declare @sql varchar(8000)
set @sql = 'select t.* ,m.金额 本日收款 , t.本日合计 - m.金额 本日结算 from (select 时间 '
select @sql = @sql + ' , sum(case 类型 when ' ' ' + 类型 + ' ' ' then 数量 else 0 end) [ ' + 类型 + '数量] '
+ ' , sum(case 类型 when ' ' ' + 类型 + ' ' ' then 金额 else 0 end) [ ' + 类型 + '金额] '
from (select distinct 类型 from t1) as a
set @sql = @sql + ' ,sum(金额) 本日合计 from t1 group by 时间) t,(select 时间 , sum(金额) 金额 from t2 group by 时间) m where t.时间 = m.时间 '
exec(@sql)
drop table t1,t2
/*
时间 a数量 a金额 b数量 b金额 c数量 c金额 本日合计 本日收款 本日结算
---------- ----- ----- ----- ----- ----- ----- -------- -------- -----------
2007-01-01 5 700 0 0 0 0 700 500 200
2007-01-02 10 1400 6 700 0 0 2100 700 1400
2007-01-03 0 0 0 0 9 600 600 400 200
*/