日期:2014-05-16 浏览次数:20708 次
id 1F 2F 3F 4F 5F 6F 7F 8F 9F 10F
1 01 08 07 04 29 24 02 18 15 03
2 05 20 09 06 NULL 25 19 33 NULL 11
3 10 23 13 26 NULL NULL NULL NULL NULL 12
4 14 NULL 17 NULL NULL NULL NULL NULL NULL 27
5 16 NULL 21 NULL NULL NULL NULL NULL NULL 28
6 30 NULL 22 NULL NULL NULL NULL NULL NULL 31
7 NULL NULL NULL NULL NULL NULL NULL NULL NULL 32
SELECT rowno=ROW_NUMBER() OVER(ORDER BY ID), * FROM(SELECT [ID], 9F,10F FROM #temp ) a
UNPIVOT([id] FOR [nF] IN(9F,10F)) b
'03','11','12','15','27','28','31','32'
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-11 11:23:18
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据#temp
if object_id('tempdb.dbo.#temp') is not null drop table #temp
go
create table #temp([id] nvarchar(4),[1F] nvarchar(4),[2F] nvarchar(4),[3F] nvarchar(4),[4F] nvarchar(4),[5F] int,[6F] int,[7F] nvarchar(4),[8F] nvarchar(4),[9F] nvarchar(4),[10F] nvarchar(4))
insert #temp
select 1,'01','08','07','04',29,24,'02',18,15,'03' union all
select 2,'05','20','09','06',null,25,'19',33,null,'11' union all
select 3,'10','23','13','26',null,null,null,null,null,'12' union all
select 4,'14',null,'17',null,null,null,null,null,null,'27' union all
select 5,'16',null,'21',null,null,null,null,null,null,'28' union all
select 6,'30',null,'22',null,null,null,null,null,null,'31' union all
select 7,null,null,null,null,null,null,null,null,null,'32'
--------------生成数据--------------------------
;WITH YM AS
(
SELECT ID1,NF
FROM
(SELECT [ID], [9F],[10F] FROM #temp ) a
UNPIVOT([ID1] FOR [nF] IN([9F],[10F])) b
)
select DISTINCT
stuff((select ','+''''+ID1+'''' from ym b ORDER BY ID1
--where b.col1=a.col1 and b.col2=a.col2
for xml path('')),1,1,'') 'ID1'
from ym a
----------------结果----------------------------
/*