Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

Getting started with PIVOT Queries in SQL Server 2005/2008

Aug 3 2010 11:12AM by Jacob Sebastian   

This post intends to help TSQL developers get started with PIVOT/CROSS TAB queries. Most business applications will need some sort of PIVOT queries and I am sure many of you must have come across pivoting requirements several times in the past.

Let us say for example we need a result set as following

SQL Server PIVOT example 

 

from a table which has data as follows.

SQL Server PIVOT Example sample data 

This essentially means the task is to generate a tabular result set from the rows that we have. With SQL Server 2000, most of us might have used #TEMP tables where we update the data temporarily and SELECTED from. Another option that most of the developers might have used with SQL Server 2000 is the CASE statement. In the example above, you can use a series of 12 CASE statements which can take for each column.

SQL Server 2005 has a handy PIVOT operator which can help us with the above task. The PIVOT clause generates a resultset as shown in the example above. For the purpose of this example, let us create a table as follows.

CREATE TABLE invoice (
    InvoiceNumber VARCHAR(20),
    invoiceDate DATETIME,
    InvoiceAmount MONEY 
)

The above SQL statement creates a table for the purpose of this example. Now let us insert some data into it.

INSERT INTO invoice
SELECT 'INV001', '2005-01-01', 100 UNION ALL
SELECT 'INV002', '2005-02-01', 40 UNION ALL
SELECT 'INV003', '2005-03-01', 60 UNION ALL
SELECT 'INV004', '2005-03-10', 15 UNION ALL
SELECT 'INV005', '2005-04-01', 50 UNION ALL
SELECT 'INV006', '2005-05-01', 77 UNION ALL
SELECT 'INV007', '2005-06-01', 12 UNION ALL
SELECT 'INV008', '2005-06-05', 56 UNION ALL
SELECT 'INV009', '2005-07-01', 34 UNION ALL
SELECT 'INV010', '2005-08-01', 76 UNION ALL
SELECT 'INV011', '2005-09-01', 24 UNION ALL
SELECT 'INV012', '2005-09-20', 10 UNION ALL
SELECT 'INV013', '2005-10-01', 15 UNION ALL
SELECT 'INV014', '2005-11-01', 40 UNION ALL
SELECT 'INV015', '2005-11-15', 21 UNION ALL
SELECT 'INV016', '2005-12-01', 17 UNION ALL
SELECT 'INV017', '2006-01-01', 34 UNION ALL
SELECT 'INV018', '2006-02-01', 24 UNION ALL
SELECT 'INV019', '2006-03-01', 56 UNION ALL
SELECT 'INV020', '2006-03-10', 43 UNION ALL
SELECT 'INV021', '2006-04-01', 24 UNION ALL
SELECT 'INV022', '2006-05-01', 11 UNION ALL
SELECT 'INV023', '2006-06-01', 6 UNION ALL
SELECT 'INV024', '2006-06-05', 13

Let us create the PIVOT query now which will return a result set as per what we needed.

SELECT *
FROM (
    SELECT 
        year(invoiceDate) as [year], 
        left(datename(month,invoicedate),3)as [month], 
        InvoiceAmount as Amount 
    FROM Invoice
) as s
PIVOT
(
    SUM(Amount) FOR [month] IN (
        jan, feb, mar, apr, 
        may, jun, jul, aug, sep, oct, nov, dec
    )
)AS p

The above query generates a result set that we needed. Let me explain the structure of the query. The first part of the query

SELECT *
FROM (
    SELECT 
        year(invoiceDate) as [year], 
        left(datename(month,invoicedate),3)as [month], 
        InvoiceAmount as Amount 
    FROM Invoice
) as s 

The first part of the query generates a subtable (view) as per the following.

 SQL Server PIVOT Example intermediate data

The second part of the query generates a PIVOT table from the above result set.

SELECT *
FROM (
    SELECT 
        year(invoiceDate) as [year], 
        left(datename(month,invoicedate),3)as [month], 
        InvoiceAmount as Amount 
    FROM Invoice
) as s
PIVOT
(
    SUM(Amount)
    FOR [month] IN (jan, feb, mar, apr, 
    may, jun, jul, aug, sep, oct, nov, dec)
)AS p

The above query returns a result set as follows.

SQL Server PIVOT output 

 

Hope this helps! If you found this useful or have a question, please post a comment to let me know about it.

First published on 18 June 2006

Tags: PIVOT, TSQL, SQLSERVER, BRH, #TSQL, #DBA, #SQLSERVER, #PIVOT,


Jacob Sebastian
1 · 100% · 32004
2
 
0
Lifesaver
 
 
 
0
Incorrect



Submit

7  Comments  

  • Here is the dynamic Pivot http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

    commented on Jan 29 2010 4:13AM
    Madhivanan
    3 · 39% · 12440
  • Hello!

    Thank-you for this great article. I was reading another article on this site (http://beyondrelational.com/blogs/madhivanan/archive/2010/08/02/values-clause-in-sql-server-2008.aspx) and that had a reference to the UNPIVOT operator.

    I ran the last few queries mentioned in that article, and noticed that in the Execution plan, the CASE & VALUES clauses used 5% each; while UNPIVOT was at 21% - indicating PIVOT & UNPIVOT were very performance intensive which I find very interesting.

    commented on Aug 12 2010 1:39PM
    Nakul Vachhrajani
    4 · 33% · 10587
  • This is a Dynamic PIVOT which can be used if there are unknown number of values http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

    commented on Sep 6 2010 3:03AM
    Madhivanan
    3 · 39% · 12440
  • Thanks to all your sharings

    commented on Mar 2 2012 11:06PM
    Guru Samy
    9 · 16% · 4992
  • HI Jacob, i have did small changes in your scripts but its giving Syntax error

    SELECT    *
    FROM     
    ( 
    SELECT YEAR(invoiceDate) AS YEAR1,MONTH(invoiceDate) AS [month],INVOICEAMOUNT AS AMT FROM invoice
    ) AS S
    pivot
    (
     sum(AMT) FOR [month] IN(1,2,3,4,5,6,7,8,9,10,11,12)
    ) as p
    
    commented on Jan 2 2013 1:09AM
    Bala Krishna
    85 · 2% · 642
  • First of all, it was interesting to revisit this post after almost 7 years since writing this :)

    In the above query, when you apply the PIVOT, the column names will be numbers (such as 1, 2, etc). In such a case you need to qualify them with a square bracket or double quotes. The following works:

    SELECT		
    	*
    FROM     
    ( 
    	SELECT 
    		YEAR(invoiceDate) AS YEAR1,
    		MONTH(invoiceDate) AS [month],
    		INVOICEAMOUNT AS AMT 
    	FROM invoice
    ) AS S
    pivot
    (
    	sum(AMT) FOR [month] IN(
    		[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
    	)
    ) as p
    
    commented on Jan 2 2013 10:04AM
    Jacob Sebastian
    1 · 100% · 32004
  • Hi Jacob Good Morning,

    Thanks a lot. Its working now.

    commented on Jan 2 2013 10:53PM
    Bala Krishna
    85 · 2% · 642

Your Comment


Sign Up or Login to post a comment.

"Getting started with PIVOT Queries in SQL Server 2005/2008" rated 5 out of 5 by 2 readers
Getting started with PIVOT Queries in SQL Server 2005/2008 , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]