计划表:商品编号 日期 计划数量
001 2009-1-1 50
002 2009-2-1 90
001 2009-4-1 10
实际用量表:商品编号 日期 实际数量
001 2009-1-1 30
002 2009-2-2 80
001 2009-4-1 30
要求结果格式如下:
以商品编号 为基准 ,以 日期 为交叉的 数量 的表达方式:
2009-1-1 2009-2-1 2009-2-2 2009-4-1
计划 实际 计划 实际 计划 实际 计划 实际 计划合计 实际合计
001 50 30 0 0 0 0 10 10 60 40002 0 0 90 0 0 80 0 0 90 80
001 2009-1-1 50
002 2009-2-1 90
001 2009-4-1 10
实际用量表:商品编号 日期 实际数量
001 2009-1-1 30
002 2009-2-2 80
001 2009-4-1 30
要求结果格式如下:
以商品编号 为基准 ,以 日期 为交叉的 数量 的表达方式:
2009-1-1 2009-2-1 2009-2-2 2009-4-1
计划 实际 计划 实际 计划 实际 计划 实际 计划合计 实际合计
001 50 30 0 0 0 0 10 10 60 40002 0 0 90 0 0 80 0 0 90 80
--> 测试数据:[TA]
if object_id('[TA]') is not null drop table [TA]
create table [TA]([商品编号] varchar(3),[日期] datetime,[计划数量] int)
insert [TA]
select '001','2009-1-1',50 union all
select '002','2009-2-1',90 union all
select '001','2009-4-1',10if object_id('[TB]') is not null drop table [TB]
create table [TB]([商品编号] varchar(3),[日期] datetime,[实际数量] int)
insert [TB]
select '001','2009-1-1',30 union all
select '002','2009-2-2',80 union all
select '001','2009-4-1',30
declare @s varchar(8000)
set @s='select ''商品编号'' '
select @s=isnull(@s+',','')+''''+ max(convert(varchar(10),日期,120)) +'''' from (select 日期 from TA union all
select 日期 from TB ) A group by 日期
set @s=@s+','' '','' '' union all select 商品编号'
select @s=isnull(@s+',','')+'
[计划 实际]=left(max(case when 日期='''+convert(varchar(10),日期,120)+''' and flag=0 then isnull(计划数量,0) else 0 end),20)
+'' ''+ left(max(case when 日期='''+convert(varchar(10),日期,120)+''' and flag=1 then isnull(计划数量,0) else 0 end),20)'
from (select *,flag=0 from TA union all
select *,flag=1 from TB ) A
group by 日期
order by 日期
exec(@s+',计划合计=sum(case when flag=0 then isnull(计划数量,0) else 0 end ),
实际合计=sum(case when flag=1 then isnull(计划数量,0) else 0 end ) from (select *,flag=0 from TA union all
select *,flag=1 from TB ) A group by 商品编号')/*
-------- ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ ----------- -----------
商品编号 2009-01-01 2009-02-01 2009-02-02 2009-04-01 0 0
001 50 30 0 0 0 0 10 30 60 60
002 0 0 90 0 0 80 0 0 90 80*/drop table TA,TB
--> 测试数据:[TA]
if object_id('[TA]') is not null drop table [TA]
create table [TA]([商品编号] varchar(3),[日期] datetime,[计划数量] int)
insert [TA]
select '001','2009-1-1',50 union all
select '002','2009-2-1',90 union all
select '001','2009-4-1',10if object_id('[TB]') is not null drop table [TB]
create table [TB]([商品编号] varchar(3),[日期] datetime,[实际数量] int)
insert [TB]
select '001','2009-1-1',30 union all
select '002','2009-2-2',80 union all
select '001','2009-4-1',30
declare @s varchar(8000)
set @s='select ''商品编号'' '
select @s=isnull(@s+',','')+''''+ max(convert(varchar(10),日期,120)) +'''' from (select 日期 from TA union all
select 日期 from TB ) A group by 日期
set @s=@s+',''计划合计'',''实际合计'' union all select 商品编号'
select @s=isnull(@s+',','')+'
[计划 实际]=left(max(case when 日期='''+convert(varchar(10),日期,120)+''' and flag=0 then isnull(计划数量,0) else 0 end),20)
+'' ''+ left(max(case when 日期='''+convert(varchar(10),日期,120)+''' and flag=1 then isnull(计划数量,0) else 0 end),20)'
from (select *,flag=0 from TA union all
select *,flag=1 from TB ) A
group by 日期
order by 日期
exec(@s+',left(sum(case when flag=0 then isnull(计划数量,0) else 0 end ),6),
left(sum(case when flag=1 then isnull(计划数量,0) else 0 end ),6) from (select *,flag=0 from TA union all
select *,flag=1 from TB ) A group by 商品编号')/*
-------- ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ ------------ ------------
商品编号 2009-01-01 2009-02-01 2009-02-02 2009-04-01 计划合计 实际合计
001 50 30 0 0 0 0 10 30 60 60
002 0 0 90 0 0 80 0 0 90 80*/drop table TA,TB