Loading ...
Sorry, an error occurred while loading the content.

TSQL - Transforming rows to columns and columns to rows

Expand Messages
  • jacob.sebastian
    Transforming rows to columns (PIVOT/CROSS TAB) and columns to rows (UNPIVOT) may be one of the common requirements that all of us must have seen several times
    Message 1 of 1 , Sep 29, 2008
    • 0 Attachment
      Transforming rows to columns (PIVOT/CROSS TAB) and columns to rows
      (UNPIVOT) may be one of the common requirements that all of us must
      have seen several times in our programming life. SQL Server 2005
      introduced two new operators: PIVOT and UNPIVOT that made writing
      cross-tab queries easier.

      I have posted an example that transforms rows to columns using the
      PIVOT operator here:
      http://www.sqlserverandxml.com/2008/09/pivot-cross-tab-sql-server-2005.html

      PIVOT operator is available only in SQL Server 2005/2008. It does not
      exists in SQL Server 2000. I have posted another version of the above
      sample code that works in SQL Server 2000. You can find the post here:
      http://www.sqlserverandxml.com/2008/09/pivot-cross-tab-sql-server-2000.html

      I have posted one more example, that performs the reverse operation
      (columns to rows) using UNPIVOT. You can find it here:
      http://www.sqlserverandxml.com/2008/09/unpivot-sql-server-2005.html

      Regards
      Jacob Sebastian, SQL Server MVP
    Your message has been successfully submitted and would be delivered to recipients shortly.