Guest User

Untitled

a guest
Jan 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. RptLOB1 RptLOB2 Correlation
  2. AE AE 1
  3. Bail AE 0.35
  4. Commercial Bail 0.25
  5. Commercial AE 0.15
  6.  
  7. AE Bail Commercial
  8. AE 1 0.35 0.15
  9. Bail 0.35 1 0.25
  10. Commercial 0.15 0.25 1
  11.  
  12. col1 col2 col3 col4 col5
  13.  
  14. Generic
  15. Company Inputs Insurance Stochastic Model Correlations Exposure Correlation Matrix
  16. AE Bail Commercial
  17. AE 1 0.35 0.15
  18. Bail 0.35 1 0.25
  19. Commercial 0.15 0.25 1
  20.  
  21. select *
  22. from
  23. (
  24. select RptLOB1 RptLOB1, RPTLOB2 RPTLOB2, Correlation
  25. from yourtable
  26. union all
  27. select RPTLOB2, RptLOB1, Correlation
  28. from yourtable
  29. union all
  30. select distinct RptLOB1, RptLOB1, 1.0
  31. from yourtable
  32. ) x
  33. pivot
  34. (
  35. max(Correlation)
  36. for RPTLOB2 in ([AE], [Bail], [Commercial])
  37. ) p;
  38.  
  39. DECLARE @query AS NVARCHAR(MAX),
  40. @colsPivot as NVARCHAR(MAX)
  41.  
  42. select @colsPivot = STUFF((SELECT distinct ','
  43. + quotename(RptLOB1)
  44. from yourtable t
  45. FOR XML PATH(''), TYPE
  46. ).value('.', 'NVARCHAR(MAX)')
  47. ,1,1,'')
  48.  
  49. set @query
  50. = 'select RptLOB1, '+@colspivot+ '
  51. from
  52. (
  53. select RptLOB1 RptLOB1, RPTLOB2 RPTLOB2, Correlation
  54. from yourtable
  55. union all
  56. select RPTLOB2, RptLOB1, Correlation
  57. from yourtable
  58. union all
  59. select distinct RptLOB1, RptLOB1, 1.0
  60. from yourtable
  61. ) x
  62. pivot
  63. (
  64. max(Correlation)
  65. for RPTLOB2 in ('+ @colspivot +')
  66. ) p'
  67.  
  68. exec(@query)
  69.  
  70. DECLARE @query AS NVARCHAR(MAX),
  71. @colsPivot as NVARCHAR(MAX),
  72. @colsRow as NVARCHAR(MAX),
  73. @colsConverted as NVARCHAR(MAX)
  74.  
  75. select @colsPivot = STUFF((SELECT distinct ','
  76. + quotename(RptLOB1)
  77. from yourtable t
  78. FOR XML PATH(''), TYPE
  79. ).value('.', 'NVARCHAR(MAX)')
  80. ,1,1,'')
  81.  
  82. select @colsRow = STUFF((SELECT distinct ', '''
  83. + RptLOB1 + ''' as ' + RptLOB1
  84. from yourtable t
  85. FOR XML PATH(''), TYPE
  86. ).value('.', 'NVARCHAR(MAX)')
  87. ,1,1,'')
  88.  
  89. select @colsConverted
  90. = STUFF((SELECT distinct ', CAST('
  91. + quotename(RptLOB1)
  92. + ' as varchar(50))'
  93. from yourtable t
  94. FOR XML PATH(''), TYPE
  95. ).value('.', 'NVARCHAR(MAX)')
  96. ,1,1,'')
  97.  
  98.  
  99. set @query
  100. = 'select ''RptLOB1'' as RptLOB1,
  101. '+ @colsRow + '
  102. union all
  103. select RptLOB1, '+ @colsConverted+ '
  104. from
  105. (
  106. select RptLOB1 RptLOB1, RPTLOB2 RPTLOB2, Correlation
  107. from yourtable
  108. union all
  109. select RPTLOB2, RptLOB1, Correlation
  110. from yourtable
  111. union all
  112. select distinct RptLOB1, RptLOB1, 1.0
  113. from yourtable
  114. ) x
  115. pivot
  116. (
  117. max(Correlation)
  118. for RPTLOB2 in ('+ @colspivot +')
  119. ) p'
  120.  
  121. exec(@query)
  122.  
  123. with d as (
  124. select RptLOB1, RptLOB2, Correlation from t union all
  125. select RptLOB2, RptLOB1, Correlation from t union all
  126. select distinct RptLob1, RptLob1, 1.0 from t
  127. )
  128. select RptLOB1, RptLOB2, Corr
  129. from d
  130. pivot (max(correlation) for val in ('AE', 'Bail', 'Commercial')) as corr
Add Comment
Please, Sign In to add comment