Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / SQL
Tip/Trick

Simple Way To Use Pivot In SQL Query

Rate me:
Please Sign up or sign in to vote.
4.73/5 (96 votes)
4 Dec 2015CPOL 1.6M   89   59
Transforming data from row-level data to columnar data.

Introduction

This is a very simple example of Pivot query for the beginners. We use pivot queries when we need to transform data from row-level to columnar data.

Pivot query help us to generate an interactive table that quickly combines and compares large amounts of data. We can rotate its rows and columns to see different summaries of the source data, and we can display the details for areas of interest at a glance. It also help us to generate Multidimensional reporting.

Background

This post intends to help T-SQL developers get started with PIVOT 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. 

Using the Code

Let us have a table name Invoice which has three properties, InvoiceNumber, InvoiceDate, InvoiceAmount. Suppose we have several rows input in the table. Our goal is to display the sum of InvoiceAmount each month.

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

Image 1

SQL
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 pvt

Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Bangladesh Bangladesh
Maksud Saifullah Pulak is a Software Engineer. He have been working more than 5 years of professional experience in Software Development industry. Currently he is working with project management and development of several software projects from his country.Technical expertise ASP.NET, C#, .NET,Visual Studio, Sql Server,Data Structure.

Comments and Discussions

 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak19-Sep-13 22:38
Maksud Saifullah Pulak19-Sep-13 22:38 
GeneralMy vote of 5 Pin
shreeniwas kushwah22-Aug-13 19:45
shreeniwas kushwah22-Aug-13 19:45 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak25-Aug-13 22:27
Maksud Saifullah Pulak25-Aug-13 22:27 
QuestionMy doubt Pin
daranee7311-Aug-13 23:32
daranee7311-Aug-13 23:32 
GeneralMy vote of 5 Pin
MohamedKamalPharm19-Jun-13 21:02
MohamedKamalPharm19-Jun-13 21:02 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak19-Jun-13 21:39
Maksud Saifullah Pulak19-Jun-13 21:39 
GeneralMy vote of 5 Pin
Asif Md. Akhlak30-Nov-12 20:58
Asif Md. Akhlak30-Nov-12 20:58 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 21:03
Maksud Saifullah Pulak30-Nov-12 21:03 
Thank You.
GeneralMy vote of 5 Pin
Monjurul Habib30-Nov-12 6:34
professionalMonjurul Habib30-Nov-12 6:34 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 7:08
Maksud Saifullah Pulak30-Nov-12 7:08 
GeneralMy vote of 5 Pin
samiatcodeproject30-Nov-12 3:39
samiatcodeproject30-Nov-12 3:39 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 4:49
Maksud Saifullah Pulak30-Nov-12 4:49 
GeneralNice tip Pin
Shahriar Iqbal Chowdhury/Galib29-Nov-12 10:53
professionalShahriar Iqbal Chowdhury/Galib29-Nov-12 10:53 
GeneralRe: Nice tip Pin
Maksud Saifullah Pulak30-Nov-12 4:50
Maksud Saifullah Pulak30-Nov-12 4:50 
GeneralMy vote of 5 Pin
Rahul Bormon29-Nov-12 7:31
Rahul Bormon29-Nov-12 7:31 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 4:51
Maksud Saifullah Pulak30-Nov-12 4:51 
GeneralMy vote of 5 Pin
Sk. Tajbir29-Nov-12 7:23
Sk. Tajbir29-Nov-12 7:23 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 4:51
Maksud Saifullah Pulak30-Nov-12 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.