--行转列

being21 2008-04-08 04:11:46
select '车辆[68]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[71]状态[分配任务]' as '状态','中区' as '区域' union all
select '车辆[66]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[0840]状态[抢救转送]' as '状态','通州' as '区域' union all
select '车辆[8781]状态[驶向现场]' as '状态','朝阳' as '区域' union all
select '车辆[168]状态[抢救转送]' as '状态','北区' as '区域' union all
select '车辆[0839]状态[驶向现场]' as '状态','房山' as '区域' union all
select '车辆[173]状态[抢救转送]' as '状态','西区' as '区域' union all
select '车辆[0366]状态[收到指令]' as '状态','大兴' as '区域' union all
select '车辆[0960]状态[收到指令]' as '状态','通州' as '区域' union all
select '车辆[0930]状态[抢救转送]' as '状态','通州' as '区域' union all
select '车辆[79]状态[分配任务]' as '状态','南区' as '区域' union all
select '车辆[007]状态[分配任务]' as '状态','中区' as '区域'

-- 结果
--
-- 丰台 房山 朝阳 /
-- 车辆[8970]状态[抢救转送] / / /
-- 车辆[9358]状态[分配任务] / / /
-- 车辆[1033]状态[抢救转送] / / /
...全文
77 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
being21 2008-04-08
对不起诸位了!
回复
-狙击手- 2008-04-08
create table ta(col1  varchar(30),col2 varchar(10))
insert ta
select '车辆[68]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[71]状态[分配任务]' as '状态','中区' as '区域' union all
select '车辆[66]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[0840]状态[抢救转送]' as '状态','通州' as '区域' union all
select '车辆[8781]状态[驶向现场]' as '状态','朝阳' as '区域' union all
select '车辆[168]状态[抢救转送]' as '状态','北区' as '区域' union all
select '车辆[0839]状态[驶向现场]' as '状态','房山' as '区域' union all
select '车辆[173]状态[抢救转送]' as '状态','西区' as '区域' union all
select '车辆[0366]状态[收到指令]' as '状态','大兴' as '区域' union all
select '车辆[0960]状态[收到指令]' as '状态','通州' as '区域' union all
select '车辆[0930]状态[抢救转送]' as '状态','通州' as '区域' union all
select '车辆[79]状态[分配任务]' as '状态','南区' as '区域' union all
select '车辆[007]状态[分配任务]' as '状态','中区' as '区域'
go
declare @s varchar(8000)

select @s = isnull(@s+',','') + '['+col2+']=max(case when col2 = '''+col2+''' then col1 else null end)'
from (select distinct col2 from ta ) a
--print @s

exec('select right(col1,6) as 状态 ,'+@s+' from ta group by right(col1,6)')

drop table ta

/*
状态 北区 朝阳 大兴 房山 南区 通州 西区 中区
------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------
[分配任务] NULL NULL NULL NULL 车辆[79]状态[分配任务] NULL NULL 车辆[71]状态[分配任务]
[抢救转送] 车辆[168]状态[抢救转送] NULL NULL NULL NULL 车辆[0930]状态[抢救转送] 车辆[173]状态[抢救转送] 车辆[68]状态[抢救转送]
[驶向现场] NULL 车辆[8781]状态[驶向现场] NULL 车辆[0839]状态[驶向现场] NULL NULL NULL NULL
[收到指令] NULL NULL 车辆[0366]状态[收到指令] NULL NULL 车辆[0960]状态[收到指令] NULL NULL

*/
回复
wlinglong 2008-04-08

给你个实例
--示例数据:
CREATE TABLE tb(ID varchar(10),NGNO char(1),QTY int)
INSERT tb SELECT '200505','A',10
UNION ALL SELECT '200505','B',20
UNION ALL SELECT '200505','C',30
UNION ALL SELECT '200506','B',10
UNION ALL SELECT '200506','C',20
UNION ALL SELECT '200506','D',30
UNION ALL SELECT '200506','E',40
GO

交叉数据报表要求1:
按ID列分组,将NGNO列水平显示为多列,但每列由NGNO的大小顺序决定,而不是由NGNO的值决定,对于示例数据,最终的结果要求如下:

ID col1 col2 col3 col4
---------- ------- ------- ------- -------
200505 A(10) B(20) C(30)
200506 B(10) C(20) D(30) E(40)

--实现代码
DECLARE @i varchar(10),@s nvarchar(4000)
SELECT TOP 1 @s='',@I=COUNT(*) FROM tb
GROUP BY ID
ORDER BY COUNT(*) DESC
WHILE @i>0
SELECT @s=N',col'+@i
+N'=ISNULL(MAX(CASE GID WHEN '+@i
+N' THEN QTY END),'''')'+@s,
@i=@i-1
EXEC(N'SELECT ID'+@s+N'
FROM(
SELECT ID,QTY=RTRIM(NGNO)+QUOTENAME(QTY,N''()''),
GID=(SELECT COUNT(DISTINCT NGNO) FROM TB WHERE ID=a.ID AND NGNO<=a.NGNO)
FROM tb a
)a GROUP BY ID')
GO

交叉数据报表要求2:
按ID列分组,将NGNO及QTY列合并显示在一个列中,最终的结果要求如下:

ID Value
---------- -----------------------------------------------
200505 A(10) B(20) C(30)
200506 B(10) C(20) D(30) E(40)

(所影响的行数为 2 行)

--实现处理的函数
CREATE FUNCTION f_Str(@ID varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+SPACE(8)+RTRIM(NGNO)+QUOTENAME(QTY,'()')
FROM tb
WHERE ID=@ID
RETURN(STUFF(@r,1,8,''))
END
GO

--调用实现查询
SELECT ID,Value=dbo.f_Str(ID) FROM tb GROUP BY ID
GO
回复
being21 2008-04-08
select '车辆[42]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[47]状态[途中待命]' as '状态','中区' as '区域' union all
select '车辆[43]状态[途中待命]' as '状态','南区' as '区域' union all
select '车辆[9079]状态[收到指令]' as '状态','石景山' as '区域' union all
select '车辆[0392]状态[抢救转送]' as '状态','朝阳' as '区域' union all
select '车辆[9558]状态[分配任务]' as '状态','东城' as '区域' union all
select '车辆[7983]状态[分配任务]' as '状态','东城' as '区域' union all
select '车辆[3413]状态[收到指令]' as '状态','朝阳' as '区域' union all
select '车辆[9358]状态[分配任务]' as '状态','丰台' as '区域' union all
select '车辆[1033]状态[抢救转送]' as '状态','丰台' as '区域' union all
select '车辆[24]状态[抢救转送]' as '状态','北区' as '区域' union all
select '车辆[50]状态[抢救转送]' as '状态','东区' as '区域' union all
select '车辆[10]状态[驶向现场]' as '状态','南区' as '区域' union all
select '车辆[12]状态[抢救转送]' as '状态','西区' as '区域' union all
select '车辆[4377]状态[收到指令]' as '状态','平谷' as '区域' union all
select '车辆[0591]状态[收到指令]' as '状态','顺义' as '区域' union all
select '车辆[5030]状态[分配任务]' as '状态','顺义' as '区域' union all
select '车辆[09]状态[驶向现场]' as '状态','东区' as '区域' union all
select '车辆[1275]状态[驶向现场]' as '状态','房山' as '区域' union all
select '车辆[7824]状态[分配任务]' as '状态','朝阳' as '区域' union all
select '车辆[0346]状态[抢救转送]' as '状态','朝阳' as '区域' union all
select '车辆[0759]状态[分配任务]' as '状态','顺义' as '区域' union all
select '车辆[0942]状态[驶向现场]' as '状态','房山' as '区域' union all
select '车辆[8970]状态[抢救转送]' as '状态','丰台' as '区域' union all
select '车辆[68]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[71]状态[分配任务]' as '状态','中区' as '区域' union all
select '车辆[66]状态[抢救转送]' as '状态','中区' as '区域' union all
select '车辆[0840]状态[抢救转送]' as '状态','通州' as '区域' union all
select '车辆[8781]状态[驶向现场]' as '状态','朝阳' as '区域' union all
select '车辆[168]状态[抢救转送]' as '状态','北区' as '区域' union all
select '车辆[0839]状态[驶向现场]' as '状态','房山' as '区域' union all
select '车辆[173]状态[抢救转送]' as '状态','西区' as '区域' union all
select '车辆[0366]状态[收到指令]' as '状态','大兴' as '区域' union all
select '车辆[0960]状态[收到指令]' as '状态','通州' as '区域' union all
select '车辆[0930]状态[抢救转送]' as '状态','通州' as '区域' union all
select '车辆[79]状态[分配任务]' as '状态','南区' as '区域' union all
select '车辆[007]状态[分配任务]' as '状态','中区' as '区域'
回复
yyyyzzzz_2002 2008-04-08
参见:
/*如何有SOL语句查询
a b c
7 2 3
转换成
name values
a 7
b 2
c 3*/

use tempdb
go
if object_id('tempdb..#') is not null
drop table #

create table #(a int
,b int
,c int)
insert into #
select 7,2,3
select * from #

select name,[values]
from (select a,b,c from # ) as a
unpivot
([values] for [name]
in (a,b,c)
) as unpvt
回复
-狙击手- 2008-04-08
不好弄吧


楼主,这。。。。
回复
wzy_love_sly 2008-04-08
没看懂啊,13行状态都不一样
回复
being21 2008-04-08
^_^
回复
没看到丰台的数据

好像是典型的行列转换,找找其他贴
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-04-08 04:11
社区公告
暂无公告