34,575
社区成员
发帖
与我相关
我的任务
分享
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([id] int,[地区] nvarchar(2),[部门A一月] int,[部门A二月] int,[部门A三月] int,[部门B一月] int,[部门b二月] int,[部门b三月] int)
Insert #A
select 1,N'北京',1,2,3,4,5,6 union all
select 2,N'上海',7,8,9,10,11,12
Go
Select
id,
地区,
部门=left(Cols,3),
[一月]=MAX(case right(Cols,2) when N'一月' then 数据 end),
[二月]=MAX(case right(Cols,2) when N'二月' then 数据 end),
[三月]=MAX(case right(Cols,2) when N'三月' then 数据 end)
from
#A
UNPIVOT
(数据 FOR Cols IN([部门A一月],[部门A二月],[部门A三月],[部门B一月],[部门b二月],[部门b三月])) AS a
group by id,地区,left(Cols,3)
/*
id 地区 部门 一月 二月 三月
1 北京 部门A 1 2 3
1 北京 部门B 4 5 6
2 上海 部门A 7 8 9
2 上海 部门B 10 11 12
*/
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([id] int,[地区] nvarchar(2),[部门A一月] int,[部门A二月] int,[部门A三月] int,[部门B一月] int,[部门b二月] int,[部门b三月] int)
Insert #A
select 1,N'北京',1,2,3,4,5,6 union all
select 2,N'上海',7,8,9,10,11,12
Go
Select
id,
地区,
部门=left(Cols,3),
[一月]=MAX(case right(Cols,2) when N'一月' then 数据 end),
[一月]=MAX(case right(Cols,2) when N'二月' then 数据 end),
[一月]=MAX(case right(Cols,2) when N'三月' then 数据 end)
from
#A
UNPIVOT
(数据 FOR Cols IN([部门A一月],[部门A二月],[部门A三月],[部门B一月],[部门b二月],[部门b三月])) AS a
group by id,地区,left(Cols,3)
/*
id 地区 部门 一月 一月 一月
1 北京 部门A 1 2 3
1 北京 部门B 4 5 6
2 上海 部门A 7 8 9
2 上海 部门B 10 11 12
*/
if not object_id('A') is null
drop table A
Go
Create table A([id] int,[地区] nvarchar(2),[部门A一月] int,[部门A二月] int,[部门A三月] int,[部门B一月] int,[部门B二月] int,[部门B三月] int)
Insert A
select 1,N'北京',1,2,3,4,5,6 union all
select 2,N'上海',7,8,9,10,11,12
;WITH CTE AS
(Select
id,
地区,
部门=left(Cols,3),
月份=case right(Cols,2) when '一月' then 1 when '二月' then 2 else 3 end,
数据
from A
UNPIVOT
(数据 FOR Cols IN([部门A一月],[部门A二月],[部门A三月],[部门B一月],[部门b二月],[部门b三月]))AS S)
SELECT id,地区,部门,月份,数据 FROM CTE
-----------------------------
(12 行受影响)
id 地区 部门 月份 数据
1 北京 部门A 1 1
1 北京 部门A 2 2
1 北京 部门A 3 3
1 北京 部门B 1 4
1 北京 部门B 2 5
1 北京 部门B 3 6
2 上海 部门A 1 7
2 上海 部门A 2 8
2 上海 部门A 3 9
2 上海 部门B 1 10
2 上海 部门B 2 11
2 上海 部门B 3 12
if not object_id('tempdb..#t_Class') is null
drop table tempdb..#t_Class
Go
Create table #t_Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert #t_Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'英语',90 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
--动态:
select * from #t_Class
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from #t_Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
if not object_id('A') is null
drop table A
Go
Create table A([id] int,[地区] nvarchar(2),[部门A一月] int,[部门A二月] int,[部门A三月] int,[部门B一月] int,[部门B二月] int,[部门B三月] int)
Insert A
select 1,N'北京',1,2,3,4,5,6 union all
select 2,N'上海',7,8,9,10,11,12
select * from A
Select
id,
地区,
部门=left(Cols,3),
月份=case right(Cols,2) when N'一月' then 1 when N'二月' then 2 else 3 end,
数据
from A
UNPIVOT
(数据 FOR Cols IN([部门A一月],[部门A二月],[部门A三月],[部门B一月],[部门b二月],[部门b三月])) AS a
DROP TABLE A
---------------------------
(12 行受影响)
id 地区 部门 月份 数据
1 北京 部门A 1 1
1 北京 部门A 2 2
1 北京 部门A 3 3
1 北京 部门B 1 4
1 北京 部门B 2 5
1 北京 部门B 3 6
2 上海 部门A 1 7
2 上海 部门A 2 8
2 上海 部门A 3 9
2 上海 部门B 1 10
2 上海 部门B 2 11
2 上海 部门B 3 12
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([id] int,[地区] nvarchar(2),[部门A一月] int,[部门A二月] int,[部门A三月] int,[部门B一月] int,[部门b二月] int,[部门b三月] int)
Insert #A
select 1,N'北京',1,2,3,4,5,6 union all
select 2,N'上海',7,8,9,10,11,12
Go
Select
id,
地区,
部门=left(Cols,3),
月份=case right(Cols,2) when N'一月' then 1 when N'二月' then 2 else 3 end,
数据
from
#A
UNPIVOT
(数据 FOR Cols IN([部门A一月],[部门A二月],[部门A三月],[部门B一月],[部门b二月],[部门b三月])) AS a
/*
id 地区 部门 月份 数据
1 北京 部门A 1 1
1 北京 部门A 2 2
1 北京 部门A 3 3
1 北京 部门B 1 4
1 北京 部门b 2 5
1 北京 部门b 3 6
2 上海 部门A 1 7
2 上海 部门A 2 8
2 上海 部门A 3 9
2 上海 部门B 1 10
2 上海 部门b 2 11
2 上海 部门b 3 12
*/