UNPIVOT Use ... To read row data into column

SELECT A1.FileName, A1.ClientID, A1.StressName, A2.StressValue
FROM
(SELECT FileName,ClientID,Stress,StressName
FROM
(SELECT FileName,clientid,column5, column6,column7,column8,column9
FROM StressTestReport
Where column4 = 'Base'
) p
UNPIVOT
(StressName FOR Stress IN
(column5, column6,column7,column8,column9)
)AS unpvt) A1,

(SELECT FileName,ClientID,Stressv,StressValue
FROM
(SELECT FileName,clientid,column5, column6,column7,column8,column9
FROM StressTestReport
Where Row_ID IN (Select Row_ID + 1 from StressTestReport Where Column1 = 'Portfolio value:' )
) p
UNPIVOT
(StressValue FOR Stressv IN
(column5, column6,column7,column8,column9)
)AS unpvt)
A2

WHERE A1.clientID = A2.clientID AND A1.fileName = A2.fileName AND A1.Stress = A2.Stressv

No comments:

Post a Comment