Simple SQLServer PIVOT/Transposed query, how to write?

后端 未结 2 1312
自闭症患者
自闭症患者 2021-01-17 02:36

I have a SELECT which is returning me data in the following form...

ID             Question             Answer
1              Any Good?            Yes
1              


        
相关标签:
2条回答
  • 2021-01-17 02:43

    Have you tried something like

    SELECT  *
    FROM    (
                SELECT  ID,
                        Question,
                        Answer
                FROM    @Table1
            ) t
    PIVOT (MAX(Answer) FOR Question IN ([Any Good?],[Happy?],[Good Staff?],[Return?])) p
    
    0 讨论(0)
  • 2021-01-17 03:03

    The pivot operation requires you to use some form of aggregate, however if you will only have one value Max() will grab the max (only) value for you

    Something Like this should work

    Select *
    from Table
    Pivot
    (
        Max(answer)
        For Question In ([Any Good?],[Happy?],[Good Staff?],[Return?])
    )
    AS P
    
    0 讨论(0)
提交回复
热议问题