Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. select
  2. resnum,
  3. GroupLeaderResNum,
  4. guestname,
  5. RoomType,
  6. Property,
  7. ResStatus
  8. into #Temptest
  9. from PMSRes
  10. where roomtype = 'N/RBT'
  11. and ResStatus <> 9
  12. Order By Property, GroupLeaderResNum
  13.  
  14. DECLARE @cols AS NVARCHAR(MAX),
  15. @query AS NVARCHAR(MAX)
  16.  
  17. select
  18. @cols = STUFF((SELECT ','
  19. + QUOTENAME(case
  20. when d.col = 'DiveRes' then col+cast(seq as varchar(10))
  21. else 'DiveRes'+cast(seq as varchar(10))+'_'+col end)
  22. from
  23. (
  24. select row_number() over(partition by GroupLeaderResNum
  25. order by ResNum) seq
  26. from #Temptest
  27. ) t
  28. cross apply
  29. (
  30. select 'DiveRes', 1 union all
  31. select 'Name', 2
  32. ) d (col, so)
  33. group by col, so, seq
  34. order by seq, so
  35. FOR XML PATH(''), TYPE
  36. ).value('.', 'NVARCHAR(MAX)')
  37. ,1,1,'')
  38.  
  39. set @query = 'SELECT GroupLeaderResNum, ' + @cols + '
  40. from
  41. (
  42. select t.GroupLeaderResNum,
  43. col = case
  44. when c.col = ''DiveRes'' then col+cast(seq as varchar(10))
  45. else ''DiveRes''+cast(seq as varchar(10))+''_''+col
  46. end,
  47. value
  48. from
  49. (
  50. select GroupLeaderResNum, ResNum, GuestName,
  51. row_number() over(partition by GroupLeaderResNum
  52. order by ResNum) seq
  53. from #TempTest
  54. ) t
  55. cross apply
  56. (
  57. select ''DiveRes'', Resnum union all
  58. select ''Name'', GuestName
  59. ) c (col, value)
  60. ) x
  61. pivot
  62. (
  63. max(value)
  64. for col in (' + @cols + ')
  65. ) p '
  66.  
  67. execute sp_executesql @query;
  68.  
  69. drop table #Temptest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement