Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. ; with numbered as
  2. (
  3. select *,
  4. -- Assign number to each record
  5. -- Numbering is separate for each pair of MLISNPI and CLIENT_ID1
  6. -- Highest [Sum of desc] will get number 1 and so forth
  7. Rank() over (partition by [MLISNPI]
  8. order by [Sum of count] desc) rn
  9. from [TargettingReport]
  10. ),
  11. ctemain as (
  12. select [MLISNPI],
  13. [CLIENT_ID1],
  14. [Sum of count]
  15. from numbered
  16. )
  17.  
  18.  
  19. select * from ctemain
  20.  
  21. +----------+------------+--------------+
  22. | MLISNPI | CLIENT_ID1 | Sum of count |
  23. +----------+------------+--------------+
  24. | 03001363 | 101522 | 436 |
  25. | 03001363 | 101596 | 0 |
  26. | 03001363 | 101597 | 0 |
  27. | 03002312 | 102165 | 66 |
  28. | 03002742 | 100062 | 1 |
  29. | 03002742 | 211074 | 1229 |
  30. | 03006958 | 102235 | 21 |
  31. | 03014986 | 213926 | 5 |
  32. | 03016270 | 213143 | 3 |
  33. | 03023284 | 212876 | 44 |
  34. | 03023284 | 213801 | 55 |
  35. | 03023821 | 100218 | 0 |
  36. | 03028812 | 211144 | 133 |
  37. | 03041666 | 100236 | 346 |
  38. | 03041666 | 103164 | 65 |
  39. | 03051731 | 213402 | 157 |
  40. | 03058777 | 100572 | 28 |
  41. | 03065509 | 101632 | 29 |
  42. | 03071952 | 213632 | 6 |
  43. | 03072059 | 101506 | 4 |
  44. | 03081449 | 100087 | 398 |
  45. | 03083205 | 214311 | 7 |
  46. | 03117698 | 210178 | 203 |
  47. | 03121302 | 214008 | 9 |
  48. | 03139502 | 102179 | 1635 |
  49. | 03147455 | 216022 | 21 |
  50. | 03149204 | 211425 | 1 |
  51. | 03186883 | 215748 | 1 |
  52. | 03186883 | 215749 | 10 |
  53. | 03190331 | 212289 | 26 |
  54. | 03800285 | 101108 | 8052 |
  55. | 03800285 | 101596 | 0 |
  56. | 03800285 | 101597 | 0 |
  57. | 03800350 | 212419 | 9 |
  58. | 03800616 | 110461 | 0 |
  59. | 03800616 | 213456 | 3 |
  60. | 03802018 | 103136 | 32 |
  61. | 03803412 | 201257 | 3 |
  62. +----------+------------+--------------+
  63.  
  64. +----------+-----------+-------------+------------+-------------+-------------+----------+
  65. | MLISNPI | clientid1 | sumofcount1 | clientid 2 | sumofcount2 | client id 3 | sum of 3 |
  66. +----------+-----------+-------------+------------+-------------+-------------+----------+
  67. | 03001363 | 101522 | 436 | 101596 | 0 | 101597 | 0 |
  68. | 03002312 | 102165 | 66 | | | | |
  69. | 03002742 | 100062 | 1 | 211074 | 1229 | | |
  70. | 03006958 | 102235 | 21 | | | | |
  71. | 03014986 | 213926 | 5 | | | | |
  72. | 03016270 | 213143 | 3 | | | | |
  73. | 03023284 | 212876 | 44 | 213801 | 55 | | |
  74. +----------+-----------+-------------+------------+-------------+-------------+----------+
  75.  
  76. CREATE TABLE [dbo].[TargettingReport](
  77. [MLISNPI] [varchar](50) NULL,
  78. [IMS_PRESCRIBER_ID] [varchar](50) NULL,
  79. [CLIENT_ID1] [varchar](50) NULL,
  80. [Sum of count] [int] NULL
  81. ) ON [PRIMARY]
  82.  
  83. select *
  84. from
  85. (
  86. select MLISNPI,
  87. cast(value as int) value,
  88. case col
  89. when 'client_id1' then 'clientid_' + cast(rn as varchar(10))
  90. when 'SumofCount' then 'SumofCount_' + cast(rn as varchar(10))
  91. end col
  92. from
  93. (
  94. select MLISNPI, Client_id1, cast(SumOfCount as varchar(50)) SumofCount,
  95. row_number() over(partition by MLISNPI order by Client_id1) rn
  96. from TargettingReport
  97. ) x
  98. unpivot
  99. (
  100. value
  101. for col in (client_id1, sumofcount)
  102. ) u
  103. ) x1
  104. pivot
  105. (
  106. sum(value)
  107. for col in ([clientid_1], [SumofCount_1],
  108. [clientid_2], [SumofCount_2],
  109. [clientid_3], [SumofCount_3])
  110. ) p
  111.  
  112. DECLARE @colsPivot AS NVARCHAR(MAX),
  113. @colsUnpivot AS NVARCHAR(MAX),
  114. @query AS NVARCHAR(MAX)
  115.  
  116. SET @colsUnPivot
  117. = stuff((select ','
  118. +quotename(C.name )
  119. from sys.columns as C
  120. where C.object_id = object_id('TargettingReport') and
  121. C.name IN ('CLIENT_ID1', 'SumOfCount')
  122. for xml path('')), 1, 1, '')
  123.  
  124. SET @colsPivot
  125. = stuff((select ','+quotename(case when C.name = 'client_id1' then 'clientid' else c.name end + '_' + cast(rn as varchar(10)))
  126. from sys.columns as C
  127. cross apply
  128. (
  129. select row_number() over(partition by MLISNPI order by Client_id1) rn
  130. from TargettingReport
  131. ) x
  132. where C.object_id = object_id('TargettingReport') and
  133. C.name IN ('CLIENT_ID1', 'SumOfCount')
  134. group by name, rn
  135. order by rn
  136. for xml path('')), 1, 1, '')
  137.  
  138. set @query
  139. = '
  140. select *
  141. from
  142. (
  143. select MLISNPI,
  144. cast(value as int) value,
  145. case col
  146. when ''client_id1'' then ''clientid_'' + cast(rn as varchar(10))
  147. when ''SumofCount'' then ''SumofCount_'' + cast(rn as varchar(10))
  148. end col
  149. from
  150. (
  151. select MLISNPI, Client_id1, cast(SumOfCount as varchar(50)) SumofCount,
  152. row_number() over(partition by MLISNPI order by Client_id1) rn
  153. from TargettingReport
  154. ) x1
  155. unpivot
  156. (
  157. value
  158. for col in (' + @colsUnPivot + ')
  159. ) unpvt
  160.  
  161. ) x2
  162. pivot
  163. (
  164. sum(value)
  165. for col in(' + @colsPivot +')
  166. )p'
  167.  
  168. exec(@query)
  169.  
  170. select @cols = STUFF((SELECT distinct ',' + QUOTENAME(wagecode)
  171. FROM t1
  172. FOR XML PATH(''), TYPE
  173. ).value('.', 'NVARCHAR(MAX)')
  174. ,1,1,'')
  175.  
  176. set @query = 'SELECT empid, ' + @cols + ' from
  177. (
  178. select empid, wagecode, amount
  179. from t1
  180. ) x
  181. pivot
  182. (
  183. sum(amount)
  184. for wagecode in (' + @cols + ')
  185. ) p '
  186.  
  187. execute(@query)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement