The SQL Server PIVOT and UNPIVOT operators are powerful tools that provide an easy way to transform your data in SQL.
SQL SERVER – Example of PIVOT UNPIVOT Cross Tab Query in Different SQL Server Versions
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. My friend and…
Read MoreSQL SERVER – PIVOT and UNPIVOT Table Examples
I previously wrote two articles about PIVOT and UNPIVOT tables. I really enjoyed writing about them as it was interesting concept. One of the Jr. DBA at my organization asked me following question. “If we PIVOT any table and UNPIVOT that table do we get our original table?” I really…
Read MoreSQL SERVER – UNPIVOT Table Example
My previous article SQL SERVER – PIVOT Table Example encouraged few of my readers to ask me question about UNPIVOT table. UNPIVOT table is reverse of PIVOT Table. USE AdventureWorks GO CREATE TABLE #Pvt ([CA] INT NOT NULL, [AZ] INT NOT NULL, [TX] INT NOT NULL); INSERT INTO #Pvt ([CA],…
Read MoreSQL SERVER – PIVOT Table Example
This is quite a popular question and I have never wrote about this on my blog. A Pivot Table can automatically sort, count, and total the data stored in one table or spreadsheet and create a second table displaying the summarized data. The PIVOT operator turns the values of a specified column into column names, effectively rotating a table.