We help IT Professionals succeed at work.

Sql query syntax

Hello i have this query :


DECLARE @query NVARCHAR(4000)
SET @query = N'SELECT Parent_Key_Desc, '+
            @cols +'
            FROM
            (SELECT  t1.Parent_Key_Desc, t1.CompanyNo, t1.[tya]
            FROM   table1  AS t1) p
            PIVOT
            (
                                MAX([tya])
            FOR companyNo IN
            ( '+
            @cols +' )
            ) AS pvt
            order by Parent_Key_Desc;'



this works well.
but lets say I had one more column to pivot ? not only  MAX([tya]) but also lets say MAX([tyb])

looks like the pivot doens like I put 2 columns inside parentheses... how do we do this ?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
I think you need to create 2 pivots. 1 for tya and 1 for tyb and then join the results.
Hamed NasrRetired IT Professional
CERTIFIED EXPERT

Commented:
For one way, Nicobo's idea is correct, try it. Waiting for your comment if you need more help.

Author

Commented:
good

Explore More ContentExplore courses, solutions, and other research materials related to this topic.