Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RptLOB1 RptLOB2 Correlation
- AE AE 1
- Bail AE 0.35
- Commercial Bail 0.25
- Commercial AE 0.15
- AE Bail Commercial
- AE 1 0.35 0.15
- Bail 0.35 1 0.25
- Commercial 0.15 0.25 1
- col1 col2 col3 col4 col5
- Generic
- Company Inputs Insurance Stochastic Model Correlations Exposure Correlation Matrix
- AE Bail Commercial
- AE 1 0.35 0.15
- Bail 0.35 1 0.25
- Commercial 0.15 0.25 1
- select *
- from
- (
- select RptLOB1 RptLOB1, RPTLOB2 RPTLOB2, Correlation
- from yourtable
- union all
- select RPTLOB2, RptLOB1, Correlation
- from yourtable
- union all
- select distinct RptLOB1, RptLOB1, 1.0
- from yourtable
- ) x
- pivot
- (
- max(Correlation)
- for RPTLOB2 in ([AE], [Bail], [Commercial])
- ) p;
- DECLARE @query AS NVARCHAR(MAX),
- @colsPivot as NVARCHAR(MAX)
- select @colsPivot = STUFF((SELECT distinct ','
- + quotename(RptLOB1)
- from yourtable t
- FOR XML PATH(''), TYPE
- ).value('.', 'NVARCHAR(MAX)')
- ,1,1,'')
- set @query
- = 'select RptLOB1, '+@colspivot+ '
- from
- (
- select RptLOB1 RptLOB1, RPTLOB2 RPTLOB2, Correlation
- from yourtable
- union all
- select RPTLOB2, RptLOB1, Correlation
- from yourtable
- union all
- select distinct RptLOB1, RptLOB1, 1.0
- from yourtable
- ) x
- pivot
- (
- max(Correlation)
- for RPTLOB2 in ('+ @colspivot +')
- ) p'
- exec(@query)
- DECLARE @query AS NVARCHAR(MAX),
- @colsPivot as NVARCHAR(MAX),
- @colsRow as NVARCHAR(MAX),
- @colsConverted as NVARCHAR(MAX)
- select @colsPivot = STUFF((SELECT distinct ','
- + quotename(RptLOB1)
- from yourtable t
- FOR XML PATH(''), TYPE
- ).value('.', 'NVARCHAR(MAX)')
- ,1,1,'')
- select @colsRow = STUFF((SELECT distinct ', '''
- + RptLOB1 + ''' as ' + RptLOB1
- from yourtable t
- FOR XML PATH(''), TYPE
- ).value('.', 'NVARCHAR(MAX)')
- ,1,1,'')
- select @colsConverted
- = STUFF((SELECT distinct ', CAST('
- + quotename(RptLOB1)
- + ' as varchar(50))'
- from yourtable t
- FOR XML PATH(''), TYPE
- ).value('.', 'NVARCHAR(MAX)')
- ,1,1,'')
- set @query
- = 'select ''RptLOB1'' as RptLOB1,
- '+ @colsRow + '
- union all
- select RptLOB1, '+ @colsConverted+ '
- from
- (
- select RptLOB1 RptLOB1, RPTLOB2 RPTLOB2, Correlation
- from yourtable
- union all
- select RPTLOB2, RptLOB1, Correlation
- from yourtable
- union all
- select distinct RptLOB1, RptLOB1, 1.0
- from yourtable
- ) x
- pivot
- (
- max(Correlation)
- for RPTLOB2 in ('+ @colspivot +')
- ) p'
- exec(@query)
- with d as (
- select RptLOB1, RptLOB2, Correlation from t union all
- select RptLOB2, RptLOB1, Correlation from t union all
- select distinct RptLob1, RptLob1, 1.0 from t
- )
- select RptLOB1, RptLOB2, Corr
- from d
- pivot (max(correlation) for val in ('AE', 'Bail', 'Commercial')) as corr
Add Comment
Please, Sign In to add comment