原始日值数据
编号 年 月 日 值
52203 2005 6 1 16.9
52203 2005 6 2 16.9
52203 2005 6 3 13
52203 2005 6 4 9.6
52203 2005 6 5 10.8
52203 2005 6 6 12.1我想要转置成
编号 年 月 1日 2日 …… 30日 31日
52203 2005 6 16.9 16.9 …… 17.2 NULL
52203 2005 7 17 16.9 …… 16.8 14.5
52203 2005 8 16.3 13 …… 13.2 19.3
52203 2005 9 21.1 9.6 …… 15.9 NULL
52203 2005 10 18.9 10.8 …… 17.6 17.6
52203 2005 11 10 12.1 …… 15.3 NULL求转置2000 的 SQL语句 拜托了
数据很多~拜托大家了
编号 年 月 日 值
52203 2005 6 1 16.9
52203 2005 6 2 16.9
52203 2005 6 3 13
52203 2005 6 4 9.6
52203 2005 6 5 10.8
52203 2005 6 6 12.1我想要转置成
编号 年 月 1日 2日 …… 30日 31日
52203 2005 6 16.9 16.9 …… 17.2 NULL
52203 2005 7 17 16.9 …… 16.8 14.5
52203 2005 8 16.3 13 …… 13.2 19.3
52203 2005 9 21.1 9.6 …… 15.9 NULL
52203 2005 10 18.9 10.8 …… 17.6 17.6
52203 2005 11 10 12.1 …… 15.3 NULL求转置2000 的 SQL语句 拜托了
数据很多~拜托大家了
set @sql = 'select 编号, 年 ,月'
select @sql = @sql + ' , max(case 日 when ''' + cast(日 as varchar) + ''' then 值 else null end) [' + cast(日 as varchar) + '日]'
from (select distinct 日 from tb) as a
set @sql = @sql + ' from tb group by 编号, 年 ,月'
exec(@sql)
编号,
年,
月,
sum(case 日 when 1 then 值 end) as [1],
sum(case 日 when 2 then 值 end) as [2],
sum(case 日 when 3 then 值 end) as [3],
sum(case 日 when 4 then 值 end) as [4],
sum(case 日 when 5 then 值 end) as [5],
sum(case 日 when 6 then 值 end) as [6],
sum(case 日 when 7 then 值 end) as [7],
sum(case 日 when 8 then 值 end) as [8],
sum(case 日 when 9 then 值 end) as [9],
sum(case 日 when 10 then 值 end) as [10],
sum(case 日 when 11 then 值 end) as [11],
sum(case 日 when 12 then 值 end) as [12],
sum(case 日 when 13 then 值 end) as [13],
sum(case 日 when 14 then 值 end) as [14],
sum(case 日 when 15 then 值 end) as [15],
sum(case 日 when 16 then 值 end) as [16],
sum(case 日 when 17 then 值 end) as [17],
sum(case 日 when 18 then 值 end) as [18],
sum(case 日 when 19 then 值 end) as [19],
sum(case 日 when 20 then 值 end) as [20],
sum(case 日 when 21 then 值 end) as [21],
sum(case 日 when 22 then 值 end) as [22],
sum(case 日 when 23 then 值 end) as [23],
sum(case 日 when 24 then 值 end) as [24],
sum(case 日 when 25 then 值 end) as [25],
sum(case 日 when 26 then 值 end) as [26],
sum(case 日 when 27 then 值 end) as [27],
sum(case 日 when 28 then 值 end) as [28],
sum(case 日 when 29 then 值 end) as [29],
sum(case 日 when 30 then 值 end) as [30],
sum(case 日 when 31 then 值 end) as [31]
from
tb
group by
编号,
年,
月
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂整理人:中国风(Roy)日期:2008.06.06
******************************************************************************************************************************************************/--1、行互列
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert 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'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
动态:declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
生成静态:select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end)
from
Class
group by [Student]GO
动态:declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')生成静态:
select *
from
Class
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b生成格式:
/*
Student 数学 物理 英语 语文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
张三 87 90 82 78(2 行受影响)
*/------------------------------------------------------------------------------------------
go
--加上总成绩(学科平均分)--2000方法:
动态:declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[总成绩]=sum([Score]) from Class group by [Student]')--加多一列(学科平均分用avg([Score]))生成动态:select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end),
[总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
from
Class
group by [Student]go--2005方法:动态:declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a
pivot (max([Score]) for [Course] in('+@s+'))b ')生成静态:select
[Student],[数学],[物理],[英语],[语文],[总成绩]
from
(select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b 生成格式:/*
Student 数学 物理 英语 语文 总成绩
------- ----------- ----------- ----------- ----------- -----------
李四 77 85 65 65 292
张三 87 90 82 78 337(2 行受影响)
*/go--2、列转行
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go--2000:动态:declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序生成静态:
select *
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英语',[Score]=[英语] from Class union all
select [Student],[Course]='语文',[Score]=[语文] from Class)t
order by [Student],[Course]go
--2005:动态:declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b生成格式:
/*
Student Course Score
------- ------- -----------
李四 数学 77
李四 物理 85
李四 英语 65
李四 语文 65
张三 数学 87
张三 物理 90
张三 英语 82
张三 语文 78(8 行受影响)
*/
insert into tb values('52203', 2005, 6 ,1 ,16.9 )
insert into tb values('52203', 2005, 6 ,2 ,16.9 )
insert into tb values('52203', 2005, 6 ,3 ,13 )
insert into tb values('52203', 2005, 6 ,4 ,9.6 )
insert into tb values('52203', 2005, 6 ,5 ,10.8 )
insert into tb values('52203', 2005, 6 ,6 ,12.1 )
go--如果固定为1-31日,可以写成静态的。我这里只写了1-6,你自己补全
select 编号, 年 ,月,
max(case 日 when 1 then 值 else null end) [1日],
max(case 日 when 2 then 值 else null end) [2日],
max(case 日 when 3 then 值 else null end) [3日],
max(case 日 when 4 then 值 else null end) [4日],
max(case 日 when 5 then 值 else null end) [5日],
max(case 日 when 6 then 值 else null end) [6日]
from tb
group by 编号, 年 ,月
/*
编号 年 月 1日 2日 3日 4日 5日 6日
---------- ----------- ----------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
52203 2005 6 16.9 16.9 13.0 9.6 10.8 12.1(所影响的行数为 1 行)
*/--也可以用动态语句写
declare @sql varchar(8000)
set @sql = 'select 编号, 年 ,月'
select @sql = @sql + ' , max(case 日 when ''' + cast(日 as varchar) + ''' then 值 else null end) [' + cast(日 as varchar) + '日]'
from (select distinct 日 from tb) as a
set @sql = @sql + ' from tb group by 编号, 年 ,月'
exec(@sql)
/*
编号 年 月 1日 2日 3日 4日 5日 6日
---------- ----------- ----------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
52203 2005 6 16.9 16.9 13.0 9.6 10.8 12.1警告: 聚合或其它 SET 操作消除了空值。
*/drop table tb