Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:10:36+00:00 2026-06-12T04:10:36+00:00

I’m trying to get a correlation matrix in SQL Server and my data is

  • 0

I’m trying to get a correlation matrix in SQL Server and my data is in a table the following way:

RptLOB1     RptLOB2   Correlation
AE          AE             1
Bail        AE            0.35
Commercial  Bail          0.25
Commercial  AE            0.15

…and so on.

I want to write a code so my output looks the following way:

            AE     Bail   Commercial
AE          1      0.35      0.15
Bail        0.35    1        0.25
Commercial  0.15   0.25       1

Order of the RptLOB doesn’t matter as long as the order is the same from top to bottom and left to right on top. I’ve been trying to find a way to approach this and I’m not quite sure what the best way is. I was thinking using PIVOT but that will not output the RptLOB’s on top (they will be considered as columns in the table).

EDIT:

This output is going to be inserted in another table like so:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:10:37+00:00Added an answer on June 12, 2026 at 4:10 am

    You can use a PIVOT for this. If you know the number of columns you have to transform, then you can use a static version:

    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;
    

    see SQL Fiddle with demo

    If you have an unknown number of values to correlate, then you will want to use a dynamic version:

    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)
    

    see SQL Fiddle with demo

    Edit – based on your comments, if you want the column headers in another row, then you can use the following:

    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)
    

    see SQL Fiddle with demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.