Article ID: 175574 - Last Review: December 15, 2005 - Revision: 4.3 How to rotate a table in SQL ServerThis article was previously published under Q175574 On This PageSUMMARY
This article describes how to rotate a SQL Server table. Suppose you
have a table that is named QTRSALES. The table has the columns YEAR, QUARTER, and AMOUNT with the data in the following format (note that there is no row for the fourth quarter of 1996): Year Quarter Amount ------------------------------- 1995 1 125,000.90 1995 2 136,000.75 1995 3 212,000.34 1995 4 328,000.82 1996 3 728,000.35 1996 2 422,000.13 1996 1 328,000.82 Now, suppose you want to rotate the table so that you can see the data in the following format: YEAR Q1 Q2 Q3 Q4 ------------------------------------------------------------------- 1995 125,000.90 136,000.75 212,000.34 328,000.82 1996 328,000.82 422,000.13 728,000.35 0.00 The query that you would use to rotate the table is in the next section of this article. Sample Query to Rotate the TableHere is the query that you would use to rotate the table:Query for Large TablesFor large tables, this query will be faster:REFERENCESAPPLIES TO
| Article Translations
|
Back to the top
