Pivoting data in SQL

Mon, Feb 10, 2014

Most pivoting of data is done outside of the 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 database. Pivot tables make some data just that much easier to work with. A good example is show 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:

Sale_Date Product_Id Units_Sold
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.

 YEAR(sale_date) AS SaleYear
 ,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

The result is shown below.

SaleYear product_id JanSales FebSales MarSales
2014 701 19 5 100
2014 900 50 90 0

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.

sale_date product_id JanSales FebSales MarSales
2014-01-01 701 19 0 0
2014-01-01 900 20 0 0
2014-01-07 900 50 0 0
2014-02-01 701 0 5 0
2014-02-07 900 0 30 0
2014-02-16 900 0 60 0
2014-03-15 701 0 0 100

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.

 ,[1] ASJan_Sales 
 ,[2] ASFeb_Sales 
 ,[3] ASMar_Sales
   ,month(sale_date) assale_month 
  ) ASsource 
PIVOT(SUM(units_sold) FORsale_month IN
    )) ASpivot_table; 

Tags bi/business intelligence/learn sql/pivot table/pivoting data/sql/

Related Posts

SQL Sample data sets
Visualising SQL Statements