行转列的问题(结贴迅速啊)
现在有个行转列的问题需要请教。
a表数据
id 商品编码 位置 操作时间 操作人 操作类型
1 aa 仓库1 2012-06-01 张三 1
2 aa 仓库2 2012-06-02 李四 0
3 bb 仓库1 2012-06-01 张三 1
4 bb 仓库1 2012-06-02 李四 0
5 bb 仓库2 2012-06-03 王五 1
.。。。
现在想要的结果是
商品编码 仓库1 操作人 操作类型 仓库2 操作人 操作类型
aa 2012-06-01 张三 1 2012-06-02 李四 0
bb 2012-06-01 张三 1 2012-06-03 王五 1
其中记录3和记录4 是在相同位置操作的,取时间最早的那条记录为准
[解决办法]
--ROY 精华贴中 大把大把的案例--行列互转/******************************************************************************************************************************************************以学生成绩为例子,比较形象易懂整理人:中国风(Roy)日期:2008.06.06******************************************************************************************************************************************************/--1、行互列--> --> (Roy)生成測試數據 if not object_id('Class') is null drop table ClassGoCreate table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)Insert Classselect N'张三',N'语文',78 union allselect N'张三',N'数学',87 union allselect N'张三',N'英语',82 union allselect N'张三',N'物理',90 union allselect N'李四',N'语文',65 union allselect N'李四',N'数学',77 union allselect N'李四',N'英语',65 union allselect 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 ClassGoCreate table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)Insert Classselect N'李四',77,85,65,65 union allselect N'张三',87,90,82,78Go--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 Colidexec('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 Colidexec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')goselect Student,[Course],[Score] from Class unpivot ([Score] for [Course] in([数学],[物理],[英语],[语文]))b生成格式:/*Student Course Score------- ------- -----------李四 数学 77李四 物理 85李四 英语 65李四 语文 65张三 数学 87张三 物理 90张三 英语 82张三 语文 78(8 行受影响)*/
[解决办法]
;with f as(select * from a t where not exists(select 1 from a where 位置=t.位置 and 操作时间<t.操作时间)select 商品编码, max(case 位置 when '仓库1' then 操作时间 else null end) as '仓库1', max(case 位置 when '仓库1' then 操作人 else null end) as '操作人', max(case 位置 when '仓库1' then 操作类型 else null end) as '操作类型', max(case 位置 when '仓库2' then 操作时间 else null end) as '仓库2', max(case 位置 when '仓库2' then 操作人 else null end) as '操作人', max(case 位置 when '仓库2' then 操作类型 else null end) as '操作类型'from fgroup by 商品编码
[解决办法]
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([id] int,[商品编码] varchar(2),[位置] varchar(5),[操作时间] datetime,[操作人] varchar(4),[操作类型] int)goinsert [test]select 1,'aa','仓库1','2012-06-01','张三',1 union allselect 2,'aa','仓库2','2012-06-02','李四',0 union allselect 3,'bb','仓库1','2012-06-01','张三',1 union allselect 4,'bb','仓库1','2012-06-02','李四',0 union allselect 5,'bb','仓库2','2012-06-03','王五',1godeclare @str varchar(max)set @str=''select @str=@str+','+[位置]+'=max(case when [位置]='+QUOTENAME([位置],'''') +' then [操作时间] else '''' end),' +'操作人=max(case when [位置]='+QUOTENAME([位置],'''')+' then [操作人] else '''' end),' +'操作类型=max(case when [位置]='+QUOTENAME([位置],'''')+' then [操作类型] else '''' end)'from test group by [位置]print @strexec('select [商品编码]'+@str+' from test group by [商品编码]')/*商品编码 仓库1 操作人 操作类型 仓库2 操作人 操作类型--------------------------------------------------------aa 2012-06-01 00:00:00.000 张三 1 2012-06-02 00:00:00.000 李四 0bb 2012-06-02 00:00:00.000 张三 1 2012-06-03 00:00:00.000 王五 1*/