Click here to Skip to main content
Click here to Skip to main content

Tagged as

Using Pivot In SQL for Reporting

, 28 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Nice and fast way for reporting
Many times, we need to show some reports such as Department Wise Expense, or Subject wise marks of students where department and subject are stored in rows and we are to show all these as Columns. In all these situations, we can use SQL Pivoting.
 
For example, for Department Wise expense:
 
Suppose we store expenses as:
 
Dept ExDate       expense
_________________________
A   |  12 Jan |  10000
A   |  15 Jan |   9000
B   |  02 Feb |   8000
C   |  02 Feb |   8000
A   |  05 Feb |  10000
C   |  12 Feb |   8000

 
select ExDate, [A] as 'Store A Expense', [B] as 'Store B Expense', [C] as 'Store C Expense' from
(
select * from expenseTable
) ExpenseData
PIVOT
(
    sum(expense) for Dept in ([A],[B],[C]) 
)as FinalData
 
You can find one more detailed script here[^].
 
--Pankaj

License

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

Share

About the Author

code in play
Software Developer (Senior)
India India
4+ year experience in Microsoft technologies(C#.Net,ASP.Net, SQL server and Sharepoint).
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pinmembercode in play8-Feb-13 8:47 
GeneralReason for my vote of 3 It's a good attempt, nevertheless th... PinmemberGeorge Tryfonas11-Mar-11 3:33 
GeneralReason for my vote of 3 Good attempt.... PinmemberPravin Patil, Mumbai28-Feb-11 4:12 

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.150224.1 | Last Updated 28 Feb 2011
Article Copyright 2011 by code in play
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid