请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,求高手帮帮小弟!!
table1月份mon 部门dep 业绩yj
-------------------------------
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8table2部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部table3 (result)部门dep 一月份 二月份 三月份
--------------------------------------
01 10 null null
02 10 8 null
03 5 null 8
04 null 9 null
从table1,table2中取出如table3所列格式数据,求高手帮帮小弟!!
table1月份mon 部门dep 业绩yj
-------------------------------
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8table2部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部table3 (result)部门dep 一月份 二月份 三月份
--------------------------------------
01 10 null null
02 10 8 null
03 5 null 8
04 null 9 null
go
create table [tb]([月份mon] varchar(10),[部门dep] varchar(10),[业绩yj] int)
insert [tb] select '一月份','01',10
union all select '一月份','02',10
union all select '一月份','03',5
union all select '二月份','02',8
union all select '二月份','04',9
union all select '三月份','03',8
go
declare @s varchar(8000)
select @s=isnull(@s,'')+',max(case [月份mon] when '''+[月份mon]+''' then [业绩yj] end) ['+[月份mon]+']' from
tb group by [月份mon]
exec('select [部门dep]'+@s+' from tb group by [部门dep]')select [部门dep]
,max(case [月份mon] when '一月份' then [业绩yj] end) [一月份]
,max(case [月份mon] when '二月份' then [业绩yj] end) [二月份]
,max(case [月份mon] when '三月份' then [业绩yj] end) [三月份]
from tb
group by [部门dep]/*
部门dep 一月份 二月份 三月份
---------- ----------- ----------- -----------
01 10 NULL NULL
02 10 8 NULL
03 5 NULL 8
04 NULL 9 NULL
(4 行受影响)
*/
go
create table [tb]([月份mon] varchar(10),[部门dep] varchar(10),[业绩yj] int)
insert [tb] select '一月份','01',10
union all select '一月份','02',10
union all select '一月份','03',5
union all select '二月份','02',8
union all select '二月份','04',9
union all select '三月份','03',8
go
select [部门dep]
,max(case [月份mon] when '一月份' then [业绩yj] end) [一月份]
,max(case [月份mon] when '二月份' then [业绩yj] end) [二月份]
,max(case [月份mon] when '三月份' then [业绩yj] end) [三月份]
from tb
group by [部门dep]
create table B(dep varchar(10),dname varchar(20))
insert into B select
'01','国内业务一部' union all select
'02','国内业务二部' union all select
'03','国内业务三部' union all select
'04','国际业务部 ' create table A(mon varchar(10),dep varchar(10),业绩 int)
insert into A select
'一月份','01',10 union all select
'一月份','02',10 union all select
'一月份','03',5 union all select
'二月份','02',8 union all select
'二月份','04',9 union all select
'三月份','03',8 declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([mon])+'=max(case when [mon]='+quotename([mon],'''')+' then 业绩 else 0 end)'
from A group by [mon]
exec('select A.[dep] ,dname '+@s+' from A Join B on A.dep=B.dep group by A.[dep],dname order by A.dep')/*
dep dname 二月份 三月份 一月份
---------- -------------------- ----------- ----------- -----------
01 国内业务一部 0 0 10
02 国内业务二部 8 0 10
03 国内业务三部 0 8 5
04 国际业务部 9 0 0
*/
go
create table [tb]([月份mon] varchar(10),[部门dep] varchar(10),[业绩yj] int)
insert [tb] select '一月份','01',10
union all select '一月份','02',10
union all select '一月份','03',5
union all select '二月份','02',8
union all select '二月份','04',9
union all select '三月份','03',8
go
select [部门dep]
,max(case when [月份mon]='一月份' then [业绩yj] end) [一月份]
,max(case when [月份mon]='二月份' then [业绩yj] end) [二月份]
,max(case when [月份mon]='三月份' then [业绩yj] end) [三月份]
from tb
group by [部门dep]
sum(case when 月份mon='一月份' then 业绩yj else 0 end) as 一月份,
sum(case when 月份mon='二月份' then 业绩yj else 0 end) as 二月份,
sum(case when 月份mon='三月份' then 业绩yj else 0 end) as 三月份
from table1
left join table2 on table1.部门dep=table2.部门dep
group by table2.部门dep
这个颜色看起来太怪了。
CSDN上的帖子一大把