How to Pivot Data in SQL
How to Pivot Data in SQL
Join the DZone community and get the full member experience.Join For Free
Atomist automates your software deliver experience. It's how modern teams deliver modern software.
Pivoting data (Wikimedia Commons)
Most pivoting of data is done outside of relational SQL databases. Relational databases are rigid and the variable number of columns in a pivot table is not something that goes hand in hand with the rigid structures defined in relational databases. Pivot tables make some data just that much easier to work with. A good example is shown below, if you wanted to compare product sales between different months, this task would be far simpler to do if sales for each product are stored in one row. Pivoting in SQL is also useful when using a third party tool to do the pivot is overkill for your needs.
SQL CASE statements together with SQL aggregation functions are used to pivot data in a relational database. Given the following data:
|2014 Jan 1||701||19|
|2014 Feb 1||701||5|
|2014 Mar 15||701||100|
|2013 Jan 7||900||50|
|2013 Jan 1||900||20|
|2014 Feb 7||900||30|
|2014 Feb 16||900||60|
The SQL statement below will pivot the detail data shown above. The result is a single row for each product_id and a column for each month with the total number of units sold per month.
SELECT YEAR(sale_date) AS SaleYear ,product_id , SUM(CASE WHEN MONTH(sale_date)=1 THEN units_sold ELSE 0 END) AS JanSales , SUM(CASE WHEN MONTH(sale_date)=2 THEN units_sold ELSE 0 END) AS FebSales , SUM(CASE WHEN MONTH(sale_date)=3 THEN units_sold ELSE 0 END) AS MarSales FROM pivot_example GROUP BY YEAR(sale_date) ,product_id
The result is shown below.
The most important aspect of using this method to pivot your data in SQL is you need to know all the possible data values in the column you would like to pivot. Another way to put it is to say need to know all the resultant pivoted column names. In the example, I pivoted the sales data into units sold per month. I know that there are only 12 months in a year and hence I know all possible columns.
It is also worthwhile to pay attention to the granularity of your data as you could end up with a very sparsely populated table. To illustrate this point have a look at what the resultant table looks like if sale_date as opposed to the SalesMonth is used to group the data.
Now imagine there where several millions of rows in the sales table.
EDIT: 2014-02-12 SQL Server and Oracle do have built in pivot functions. Perhaps its just me but I have found the the syntax cumbersome and always feel I have to relearn the whole syntax every time I have used it. Below is a SQL statement that will work in SQL Server and give you the pivoted data.
SELECT product_id ,  AS Jan_Sales ,  AS Feb_Sales ,  AS Mar_Sales FROM ( SELECT product_id ,units_sold ,month(sale_date) as sale_month FROM pivot_example ) AS source PIVOT(SUM(units_sold) FOR sale_month IN (  , , )) AS pivot_table;
Published at DZone with permission of Mpumelelo Msimanga , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.