Advertisement
Guest User

Untitled

a guest
Jun 12th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. create table #myrows (id char(1), seq_i int, val char(10))
  2.  
  3. insert into #myrows values('A',1, 'A1')
  4. insert into #myrows values('A',2, 'A2')
  5. insert into #myrows values('A',3, 'A3')
  6. insert into #myrows values('A',4, 'A4')
  7. insert into #myrows values('A',5, 'A5')
  8. insert into #myrows values('A',6, 'A6')
  9. insert into #myrows values('A',7, 'A7')
  10. insert into #myrows values('A',8, 'A8')
  11. insert into #myrows values('A',9, 'A9')
  12. insert into #myrows values('A',10, 'A10')
  13.  
  14. insert into #myrows values('B',1, 'B1')
  15. insert into #myrows values('B',2, 'B2')
  16. insert into #myrows values('B',3, 'B3')
  17. insert into #myrows values('B',4, 'B4')
  18. insert into #myrows values('B',5, 'B5')
  19. insert into #myrows values('B',6, 'B6')
  20.  
  21. insert into #myrows values('C',1, 'C1')
  22. insert into #myrows values('C',2, 'C2')
  23. insert into #myrows values('C',3, 'C3')
  24.  
  25. DECLARE @max_hierarchy int
  26. DECLARE @code CHAR(1)
  27.  
  28. select @code = 'C'
  29.  
  30. SELECT @max_hierarchy = max(seq_i)
  31. FROM #myrows
  32. WHERE id=@code
  33.  
  34. SELECT top 1
  35. (SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy) AS 'Level1',
  36. (SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-1) AS 'Level2',
  37. (SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-2) AS 'Level3',
  38. (SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-3) AS 'Level4',
  39. (SELECT val from #myrows WHERE id=@code AND seq_i = @max_hierarchy-4) AS 'Level5'
  40. from #myrows
  41. WHERE id=@code
  42.  
  43. Code Level1 Level2 Level3 Level4 Level5
  44. ---- ----------- ----------- ----------- ----------- -----------
  45. A A10 A9 A8 A7 A6
  46. B B6 B5 B4 B3 B2
  47. C C3 C2 C1 NULL NULL
  48.  
  49. with cte as (
  50. select id, seq_i, val, ROW_NUMBER() over (partition by id order by seq_i desc) rn
  51. from #myrows
  52. )
  53. select
  54. id,
  55. max(case rn when 1 then val else null end) Level1,
  56. max(case rn when 2 then val else null end) Level2,
  57. max(case rn when 3 then val else null end) Level3,
  58. max(case rn when 4 then val else null end) Level4,
  59. max(case rn when 5 then val else null end) Level5
  60. from cte
  61. group by id
  62.  
  63. create table #cte (id char(1), seq_i int, val char(10), level varchar(10))
  64.  
  65. ;with cte as (
  66. select id, seq_i, val, ROW_NUMBER() over (partition by id order by seq_i desc) rn
  67. from #myrows
  68. )
  69. insert into #cte (id, seq_i, val, level)
  70. select id, seq_i, val,
  71. 'Level' + right('000' + cast(rn as varchar), 4) from cte
  72.  
  73. DECLARE @cols VARCHAR(1000)
  74. DECLARE @sqlquery VARCHAR(2000)
  75.  
  76. SELECT @cols = STUFF(( SELECT distinct ',' + QuoteName(level)
  77. FROM #cte FOR XML PATH('') ), 1, 1, '')
  78.  
  79. select @cols
  80.  
  81. SET @sqlquery = 'SELECT * FROM
  82. (SELECT id, level, val
  83. FROM #cte ) base
  84. PIVOT (max(val) FOR [level]
  85. IN (' + @cols + ')) AS finalpivot'
  86.  
  87. EXECUTE ( @sqlquery )
  88.  
  89. ;WITH PivotSource
  90. AS
  91. (
  92. SELECT a.id
  93. ,a.seq_i
  94. ,ROW_NUMBER() OVER(PARTITION BY a.id ORDER by a.seq_i DESC) row_num
  95. FROM #myrows a
  96. )
  97. SELECT pvt.id AS Code
  98. ,pvt.id + CONVERT(VARCHAR(10), pvt.[1]) AS Level1
  99. ,pvt.id + CONVERT(VARCHAR(10), pvt.[2]) AS Level2
  100. ,pvt.id + CONVERT(VARCHAR(10), pvt.[3]) AS Level3
  101. ,pvt.id + CONVERT(VARCHAR(10), pvt.[4]) AS Level4
  102. ,pvt.id + CONVERT(VARCHAR(10), pvt.[5]) AS Level5
  103. FROM PivotSource src
  104. PIVOT ( MAX(src.seq_i) FOR src.row_num IN([1], [2], [3], [4], [5]) ) pvt;
  105.  
  106. Code Level1 Level2 Level3 Level4 Level5
  107. ---- ----------- ----------- ----------- ----------- -----------
  108. A A10 A9 A8 A7 A6
  109. B B6 B5 B4 B3 B2
  110. C C3 C2 C1 NULL NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement