SQL Server PIVOT

  • How do i pivot on this Table. I want the downer table to look like the one above, with the entries in the row as values in the column.

  • malehakim - Wednesday, August 22, 2018 2:28 AM

    How do i pivot on this Table. I want the downer table to look like the one above, with the entries in the row as values in the column.

    Take a look at the Microsoft Docs for this
    Using PIVOT and UNPIVOT

  • Yes. I  looked at the Docs..and it seems that for the PIVOT to work, i need at least one field to aggregate on. In this case, am not in any need to do any aggregate. Its simply one row and i want to just transpose it.

  • malehakim - Wednesday, August 22, 2018 4:16 AM

    Yes. I  looked at the Docs..and it seems that for the PIVOT to work, i need at least one field to aggregate on. In this case, am not in any need to do any aggregate. Its simply one row and i want to just transpose it.

    I have to ask WHY?!  SQL is strongly typed and you are trying to store different types in the same column, which is almost always a bad idea.  So, what are you trying to solve that requires you to do this?

    Also, the documentation clearly states that (emphasis added)

    PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output. UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values.

    It sounds like you want to do an unpivot rather than a pivot.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply