行转列的问题(结贴迅速啊)

clxsl_huang 2012-06-26 04:33:23
现在有个行转列的问题需要请教。
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 是在相同位置操作的,取时间最早的那条记录为准
...全文
69 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
clxsl_huang 2012-06-26
  • 打赏
  • 举报
回复
oracle那边人太少就转到sqlservei这边来。
我困惑的是 操作人 操作类型 怎么带出是 最小时间的那个记录
clxsl_huang 2012-06-26
  • 打赏
  • 举报
回复
有没有oracle的写法
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test](
[id] int,
[商品编码] varchar(2),
[位置] varchar(5),
[操作时间] datetime,
[操作人] varchar(4),
[操作类型] int
)
go
insert [test]
select 1,'aa','仓库1','2012-06-01','张三',1 union all
select 2,'aa','仓库2','2012-06-02','李四',0 union all
select 3,'bb','仓库1','2012-06-01','张三',1 union all
select 4,'bb','仓库1','2012-06-02','李四',0 union all
select 5,'bb','仓库2','2012-06-03','王五',1
go
select * into #test
from test a
where a.操作时间=(select MIN(b.操作时间) from test b
where a.商品编码=b.商品编码 and a.位置=b.位置)
go
declare @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 @str
exec('select [商品编码]'+@str+' from #test group by [商品编码]')
/*
商品编码 仓库1 操作人 操作类型 仓库2 操作人 操作类型
--------------------------------------------------------------------------------------------
aa 2012-06-01 00:00:00.000 张三 1 2012-06-02 00:00:00.000 李四 0
bb 2012-06-01 00:00:00.000 张三 1 2012-06-03 00:00:00.000 王五 1
*/
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
create table [test](
[id] int,
[商品编码] varchar(2),
[位置] varchar(5),
[操作时间] datetime,
[操作人] varchar(4),
[操作类型] int
)
go
insert [test]
select 1,'aa','仓库1','2012-06-01','张三',1 union all
select 2,'aa','仓库2','2012-06-02','李四',0 union all
select 3,'bb','仓库1','2012-06-01','张三',1 union all
select 4,'bb','仓库1','2012-06-02','李四',0 union all
select 5,'bb','仓库2','2012-06-03','王五',1
go

declare @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 @str
exec('select [商品编码]'+@str+' from test group by [商品编码]')
/*
商品编码 仓库1 操作人 操作类型 仓库2 操作人 操作类型
--------------------------------------------------------------------------------------------
aa 2012-06-01 00:00:00.000 张三 1 2012-06-02 00:00:00.000 李四 0
bb 2012-06-02 00:00:00.000 张三 1 2012-06-03 00:00:00.000 王五 1
*/
--小F-- 2012-06-26
  • 打赏
  • 举报
回复
;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
f
group by
商品编码
--小F-- 2012-06-26
  • 打赏
  • 举报
回复
;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
f
group by
商品编码
Felixzhaowenzhong 2012-06-26
  • 打赏
  • 举报
回复

--ROY 精华贴中 大把大把的案例

--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂

整理人:中国风(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 行受影响)
*/

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧