22,210
社区成员
发帖
与我相关
我的任务
分享
insert into chq3g4(id,part)
select 1,'225100:02:3:20160725;225100:01:20:20160601' union all
select 2,'225100:03:1:20160701;225300:08:80:20160801;225400:09:90:20160901' union all
select 3,'225200:02:12:20160501' union all
select 4,':01:3:20160725' union all
select 5,'225200:01:2:20160401' union all
select 6,'225200:03::20160411' union all
select 7,'225300:02:3:20160725' union all
select 8,'225300:02:14:' union all
select 9,'225300:01:3:20160201' union all
select 10,'225300:01:3:20160301'
WITH RECURSIVE
cnt(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM cnt WHERE x<100)
SELECT ID,part,
substr(part,1,(INSTR(part,':')-1))AS 地区,
substr(substr(part,INSTR(part,':')+1),1,instr(substr(part,INSTR(part,':')+1),':')-1) AS 分类,
substr(substr(substr(part,INSTR(part,':')+1),instr(substr(part,INSTR(part,':')+1),':')+1),1,instr(substr(substr(part,INSTR(part,':')+1),instr(substr(part,INSTR(part,':')+1),':')+1),':')-1) AS 数量,
substr(substr(substr(substr(part,INSTR(part,':')+1),instr(substr(part,INSTR(part,':')+1),':')+1),instr(substr(substr(part,INSTR(part,':')+1),instr(substr(part,INSTR(part,':')+1),':')+1),':')+1),1,8)AS 日期
FROM (
select id, substr(a.part,case when cnt.x=0 then 1 else cnt.x+2 end,instr(a.part||';',';')) as part
from chq3g4 as a
left join cnt on (substr(a.part,cnt.x+1,1)=';' or x=0)
) as t
/*
ID 地区 分类 数量 日期
1 225100 02 3 20160725
1 225100 01 20 20160601
2 225100 03 1 20160701
2 225300 08 80 20160801
2 225400 09 90 20160901
3 225200 02 12 20160501
4 01 3 20160725
5 225200 01 2 20160401
6 225200 03 20160411
7 225300 02 3 20160725
8 225300 02 14
9 225300 01 3 20160201
10 225300 01 3 20160301
*/
WITH RECURSIVE
cnt(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM cnt WHERE x<1000)
select id, a.part, substr(a.part,case when cnt.x=0 then 1 else cnt.x+2 end,instr(a.part||';',';')-1) as part1
from chq3g4 as a
left join cnt on (substr(a.part,cnt.x+1,1)=';' or x=0)
/*
225100:02:3:20160725;225100:01:20:20160601 225100:02:3:20160725
225100:02:3:20160725;225100:01:20:20160601 225100:01:20:2016060
225100:03:1:20160701 225100:03:1:20160701
225200:02:12:20160501 225200:02:12:20160501
:01:3:20160725 :01:3:20160725
225200:01:2:20160401 225200:01:2:20160401
225200:03::20160411 225200:03::20160411
225300:02:3:20160725 225300:02:3:20160725
225300:02:14: 225300:02:14:
225300:01:3:20160201 225300:01:3:20160201
225300:01:3:20160301 225300:01:3:20160301
*/
;with tb(id,part) AS (
select 1,'225100:02:3:20160725;225100:01:20:20160601' union all
select 2,'225100:03:1:20160701' union all
select 3,'225200:02:12:20160501' union all
select 4,':01:3:20160725' union all
select 5,'225200:01:2:20160401' union all
select 6,'225200:03::20160411' union all
select 7,'225300:02:3:20160725' union all
select 8,'225300:02:14:' union all
select 9,'225300:01:3:20160201' union all
select 10,'225300:01:3:20160301'
),p AS (
select id
,r.si,DENSE_RANK()OVER(PARTITION BY id ORDER BY CONVERT(VARCHAR,r.si)) AS rn,'data'+LTRIM(ROW_NUMBER()OVER(PARTITION BY id,r.si ORDER BY d.k)) AS title
,d.v
FROM tb
CROSS APPLY(values(CONVERT(XML,'<r><n>'+replace(replace(tb.part,':','</n><n>'),';','</n></r><r><n>')+'</n></r>'))) x(n)
OUTER APPLY (SELECT s.b.query('.') AS i,s.b.value('.','varchar(500)') si FROM x.n.nodes('r')s(b)) r
OUTER APPLY (SELECT s.b.value('.','varchar(500)') AS v,ROW_NUMBER()OVER(ORDER BY GETDATE()) k FROM r.i.nodes('r/n')s(b)) d
)
--DROP TABLE #tt
SELECT * INTO #tt FROM p
SELECT * FROM #tt
DECLARE @col VARCHAR(max),@sql VARCHAR(max)
SELECT @col=ISNULL(@col+',','')+title FROM #tt GROUP BY title ORDER BY title
PRINT @col
SET @sql='
SELECT id,'+@col+' FROM #tt
PIVOT(MAX(v) FOR title IN ('+@col+')) p'
EXEC(@sql)
/*
id data1 data2 data3 data4
1 225100 01 20 20160601
1 225100 02 3 20160725
2 225100 03 1 20160701
3 225200 02 12 20160501
4 01 3 20160725
5 225200 01 2 20160401
6 225200 03 20160411
7 225300 02 3 20160725
8 225300 02 14
9 225300 01 3 20160201
10 225300 01 3 20160301
*/