--如果分类是固定的
select 单位=isnull(a.单位,b.单位)
,a. 分类1,a.分类2,a.分类3
,b.分类1数据,b.分类2数据,b.分类3数据
from(
select 单位
,分类1=max(case 分类 when '分类1' then 描述 end)
,分类2=max(case 分类 when '分类2' then 描述 end)
,分类3=max(case 分类 when '分类3' then 描述 end)
from 表1 group by 单位
)a full(
select 单位
,分类1=sum(case 分类 when '分类1数据' then 数据 end)
,分类2=sum(case 分类 when '分类2数据' then 数据 end)
,分类3=sum(case 分类 when '分类3数据' then 数据 end)
from 表2 group by 单位
)b on a.单位=b.单位
select 单位=isnull(a.单位,b.单位)
,a. 分类1,a.分类2,a.分类3
,b.分类1数据,b.分类2数据,b.分类3数据
from(
select 单位
,分类1=max(case 分类 when '分类1' then 描述 end)
,分类2=max(case 分类 when '分类2' then 描述 end)
,分类3=max(case 分类 when '分类3' then 描述 end)
from 表1 group by 单位
)a full(
select 单位
,分类1=sum(case 分类 when '分类1数据' then 数据 end)
,分类2=sum(case 分类 when '分类2数据' then 数据 end)
,分类3=sum(case 分类 when '分类3数据' then 数据 end)
from 表2 group by 单位
)b on a.单位=b.单位
select 单位=isnull(a.单位,b.单位)
,a. 分类1,a.分类2,a.分类3
,b.分类1数据,b.分类2数据,b.分类3数据
from(
select 单位
,分类1=max(case 分类 when '分类1' then 描述 end)
,分类2=max(case 分类 when '分类2' then 描述 end)
,分类3=max(case 分类 when '分类3' then 描述 end)
from 表1 group by 单位
)a full join(
select 单位
,分类1数据=sum(case 分类 when '分类1' then 数据 end)
,分类2数据=sum(case 分类 when '分类2' then 数据 end)
,分类3数据=sum(case 分类 when '分类3' then 数据 end)
from 表2 group by 单位
)b on a.单位=b.单位
go
declare @s1 varchar(8000),@s2 varchar(8000)
,@s3 varchar(8000),@s4 varchar(8000)
select @s1='',@s2='',@s3='',@s4=''
select @s1=@s1+',['+分类+']=max(case 分类 when '''+分类+''' then 描述 end)'
,@s2=@s2+',['+分类+'数据]=sum(case 分类 when '''+分类+''' then 数据 end)'
,@s3=@s3+',['+分类+']'
,@s4=@s4+',['+分类+'数据]'
from(select 分类 from 表1 union select 分类 from 表2) a
exec('select 单位=isnull(a.单位,b.单位)
'+@s3+@s4+'
from(select 单位'+@s1+' from 表1 group by 单位
)a full join(select 单位'+@s2+' from 表2 group by 单位
)b on a.单位=b.单位')
第二个才是正确的.
主要是要防止某些单位仅在一个表中出现的情况
create table 表1(单位 char(5),分类 char(5),描述 char(4))
insert into 表1
select '单位1','分类1','aaaa'
union all select '单位1','分类2','bbbb'
union all select '单位1','分类3','cccc'
union all select '单位2','分类1','dddd'
union all select '单位2','分类2','eeee'
union all select '单位2','分类3','ffff'create table 表2(单位 char(5),分类 char(5),数据 int)
insert into 表2
select '单位1','分类1',1
union all select '单位1','分类1',10
union all select '单位1','分类2',2
union all select '单位1','分类2',20
union all select '单位1','分类3',3
union all select '单位2','分类1',4
union all select '单位2','分类2',5
union all select '单位2','分类3',6
go--如果分类是固定的处理方法
select 单位=isnull(a.单位,b.单位)
,a. 分类1,a.分类2,a.分类3
,b.分类1数据,b.分类2数据,b.分类3数据
from(
select 单位
,分类1=max(case 分类 when '分类1' then 描述 end)
,分类2=max(case 分类 when '分类2' then 描述 end)
,分类3=max(case 分类 when '分类3' then 描述 end)
from 表1 group by 单位
)a full join(
select 单位
,分类1数据=sum(case 分类 when '分类1' then 数据 end)
,分类2数据=sum(case 分类 when '分类2' then 数据 end)
,分类3数据=sum(case 分类 when '分类3' then 数据 end)
from 表2 group by 单位
)b on a.单位=b.单位--如果分类是不固定的处理方法
declare @s1 varchar(8000),@s2 varchar(8000)
,@s3 varchar(8000),@s4 varchar(8000)
select @s1='',@s2='',@s3='',@s4=''
select @s1=@s1+',['+分类+']=max(case 分类 when '''+分类+''' then 描述 end)'
,@s2=@s2+',['+分类+'数据]=sum(case 分类 when '''+分类+''' then 数据 end)'
,@s3=@s3+',['+分类+']'
,@s4=@s4+',['+分类+'数据]'
from(select 分类 from 表1 union select 分类 from 表2) a
exec('select 单位=isnull(a.单位,b.单位)
'+@s3+@s4+'
from(select 单位'+@s1+' from 表1 group by 单位
)a full join(select 单位'+@s2+' from 表2 group by 单位
)b on a.单位=b.单位')
go--删除测试环境
drop table 表1,表2/*--测试结果--如果分类是固定的处理方法的测试结果单位 分类1 分类2 分类3 分类1数据 分类2数据 分类3数据
----- ---- ---- ---- ----------- ----------- -----------
单位1 aaaa bbbb cccc 11 22 3
单位2 dddd eeee ffff 4 5 6(所影响的行数为 2 行)--如果分类是不固定的处理方法的测试结果单位 分类1 分类2 分类3 分类1数据 分类2数据 分类3数据
----- ---- ---- ---- ----------- ----------- -----------
单位1 aaaa bbbb cccc 11 22 3
单位2 dddd eeee ffff 4 5 6
--*/
drop table c
create table z(dep varchar(10),lei varchar(10),text varchar(10))
create table c(dep varchar(10),lei varchar(10),num int)
insert into z select '单位1','分类1','aaaa'
union all select '单位1','分类2','bbbb'
union all select '单位1','分类3','cccc'
union all select '单位2','分类1','dddd'
union all select '单位2','分类2','eeee'
union all select '单位2', '分类3','ffff'
insert into c select
'单位1', '分类1', '1'
union all select'单位1' ,'分类1', '10'
union all select'单位1' ,'分类2', '2'
union all select'单位1' ,'分类2', '20'
union all select'单位1' ,'分类3', '3'
union all select'单位2' ,'分类1', '4'
union all select'单位2' ,'分类2', '5'
union all select'单位2' ,'分类3', '6'
select a.dep ,
max(case a.lei when '分类1' then a.text else '' end) as [分类1],
max(case a.lei when '分类2' then a.text else '' end) as [分类2],
max(case a.lei when '分类3' then a.text else '' end) as [分类3],
sum(case a.lei when '分类1' then a.a else 0 end)as 分类1数据,
sum(case a.lei when '分类2' then a.a else 0 end)as 分类2数据,
sum(case a.lei when '分类3' then a.a else 0 end)as 分类3数据
from (
select c.dep,c.lei,a=sum(c.num),z.text from c inner join z on c.dep=z.dep and c.lei=z.lei group by c.dep,c.lei,z.text
) a group by a.dep
/*
dep 分类1 分类2 分类3 分类1数据 分类2数据 分类3数据
---------- ---------- ---------- ---------- ----------- ----------- -----------
单位1 aaaa bbbb cccc 11 22 3
单位2 dddd eeee ffff 4 5 6
*/