Click here to Skip to main content
11,712,241 members (66,768 online)
Click here to Skip to main content

Tagged as

Simple Way To Use Pivot In SQL Query

, 29 Nov 2012 CPOL 982.6K 53
Rate this:
Please Sign up or sign in to vote.
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.

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

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 pivot

License

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

Share

About the Author

Maksud Saifullah Pulak
Software Developer
Bangladesh Bangladesh
Maksud Saifullah Pulak is a Software Engineer. He have been working more then 2 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.

You may also be interested in...

Comments and Discussions

 
QuestionSum for Rows and Column Pin
Member 444291617-Jul-15 3:30
memberMember 444291617-Jul-15 3:30 
QuestionThanks Pin
Narendra Singh Chauhan16-Jul-15 0:55
memberNarendra Singh Chauhan16-Jul-15 0:55 
GeneralNicely explained Pin
Member 1171243922-May-15 13:54
memberMember 1171243922-May-15 13:54 
GeneralGreat Pin
Member 1140761327-Jan-15 20:06
memberMember 1140761327-Jan-15 20:06 
GeneralIt is important Pin
Md. Sydur Rahman13-Jan-15 17:45
memberMd. Sydur Rahman13-Jan-15 17:45 
QuestionError? Most Likely This. Pin
netferret12-Dec-14 5:07
membernetferret12-Dec-14 5:07 
GeneralThanks Pin
Paulo Augusto Künzel29-Sep-14 3:30
professionalPaulo Augusto Künzel29-Sep-14 3:30 
QuestionError not sure why Pin
lgmanuel12-Aug-14 10:46
memberlgmanuel12-Aug-14 10:46 
AnswerRe: Error not sure why Pin
Nickysqlboy26-Aug-14 3:55
memberNickysqlboy26-Aug-14 3:55 
QuestionPivot Table... Pin
Harnis Findoliya30-May-14 1:41
memberHarnis Findoliya30-May-14 1:41 
GeneralMy vote of 5 Pin
lnucleus12-May-14 1:44
memberlnucleus12-May-14 1:44 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak18-May-14 1:50
memberMaksud Saifullah Pulak18-May-14 1:50 
GeneralMy vote of 5 Pin
Animesh Datta4-May-14 22:51
memberAnimesh Datta4-May-14 22:51 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak12-May-14 1:02
memberMaksud Saifullah Pulak12-May-14 1:02 
GeneralMy vote of 2 Pin
Pawan Mishra14-Apr-14 2:58
memberPawan Mishra14-Apr-14 2:58 
GeneralMy vote of 1 Pin
Karthik Chintala26-Mar-14 23:08
memberKarthik Chintala26-Mar-14 23:08 
BugAvoid keyword PIVOT Pin
Kip Bryan5-Feb-14 7:04
memberKip Bryan5-Feb-14 7:04 
GeneralRe: Avoid keyword PIVOT Pin
Maksud Saifullah Pulak3-Mar-14 3:48
memberMaksud Saifullah Pulak3-Mar-14 3:48 
Questionexplain me Pin
imtutul11-Jan-14 6:29
memberimtutul11-Jan-14 6:29 
QuestionDynamic columns Pin
Greesemonkey310-Dec-13 7:37
memberGreesemonkey310-Dec-13 7:37 
AnswerRe: Dynamic columns Pin
digimanus2-Apr-15 0:26
memberdigimanus2-Apr-15 0:26 
GeneralMy vote of 4 Pin
dexter.kumar7-Oct-13 0:50
memberdexter.kumar7-Oct-13 0:50 
GeneralRe: My vote of 4 Pin
Maksud Saifullah Pulak29-Nov-13 23:23
professionalMaksud Saifullah Pulak29-Nov-13 23:23 
GeneralMy vote of 3 Pin
Member 101968783-Oct-13 19:01
memberMember 101968783-Oct-13 19:01 
GeneralRe: My vote of 3 Pin
Maksud Saifullah Pulak5-Oct-13 7:49
professionalMaksud Saifullah Pulak5-Oct-13 7:49 
GeneralMy vote of 1 Pin
pavan bhavsar22-Sep-13 7:09
memberpavan bhavsar22-Sep-13 7:09 
GeneralMy vote of 5 Pin
rishadek12-Sep-13 1:54
memberrishadek12-Sep-13 1:54 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak19-Sep-13 22:38
professionalMaksud Saifullah Pulak19-Sep-13 22:38 
GeneralMy vote of 5 Pin
shreeniwas kushwah22-Aug-13 19:45
membershreeniwas kushwah22-Aug-13 19:45 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak25-Aug-13 22:27
professionalMaksud Saifullah Pulak25-Aug-13 22:27 
QuestionMy doubt Pin
daranee7311-Aug-13 23:32
memberdaranee7311-Aug-13 23:32 
GeneralMy vote of 5 Pin
MohamedKamalPharm19-Jun-13 21:02
professionalMohamedKamalPharm19-Jun-13 21:02 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak19-Jun-13 21:39
professionalMaksud Saifullah Pulak19-Jun-13 21:39 
GeneralMy vote of 5 Pin
Asif Md. Akhlak30-Nov-12 20:58
memberAsif Md. Akhlak30-Nov-12 20:58 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 21:03
memberMaksud Saifullah Pulak30-Nov-12 21:03 
GeneralMy vote of 5 Pin
Monjurul Habib30-Nov-12 6:34
memberMonjurul Habib30-Nov-12 6:34 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 7:08
memberMaksud Saifullah Pulak30-Nov-12 7:08 
GeneralMy vote of 5 Pin
samiatcodeproject30-Nov-12 3:39
membersamiatcodeproject30-Nov-12 3:39 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 4:49
memberMaksud Saifullah Pulak30-Nov-12 4:49 
GeneralNice tip Pin
Shahriar Iqbal Chowdhury29-Nov-12 10:53
memberShahriar Iqbal Chowdhury29-Nov-12 10:53 
GeneralRe: Nice tip Pin
Maksud Saifullah Pulak30-Nov-12 4:50
memberMaksud Saifullah Pulak30-Nov-12 4:50 
GeneralMy vote of 5 Pin
Rahul Bormon29-Nov-12 7:31
memberRahul Bormon29-Nov-12 7:31 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 4:51
memberMaksud Saifullah Pulak30-Nov-12 4:51 
GeneralMy vote of 5 Pin
Sk. Tajbir29-Nov-12 7:23
memberSk. Tajbir29-Nov-12 7:23 
GeneralRe: My vote of 5 Pin
Maksud Saifullah Pulak30-Nov-12 4:51
memberMaksud Saifullah Pulak30-Nov-12 4:51 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 29 Nov 2012
Article Copyright 2012 by Maksud Saifullah Pulak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid