Click here to Skip to main content
15,881,898 members
Articles / Database Development / SQL Server
Alternative
Tip/Trick

Using Pivot In SQL for Reporting

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
15 Mar 2011CPOL 6.8K   3   1
select ExDate, SUM(case when Dept='A' then expense else 0 end)[Store A Expense],SUM(case when Dept='B' then expense else 0 end)[Store B Expense],SUM(case when Dept='C' then expense else 0 end)[Store C Expense]from expenseTable where Dept in ('A','B','C') group by ExDate
SQL
select ExDate, SUM(case when Dept='A' then expense else 0 end)[Store A Expense],
SUM(case when Dept='B' then expense else 0 end)[Store B Expense],
SUM(case when Dept='C' then expense else 0 end)[Store C Expense]
from expenseTable where Dept in ('A','B','C') group by ExDate 

License

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


Written By
Team Leader
India India
Kannan KR, Team Lead working in a Mobile Application Development Company in Noida, India. Having 8+ years of experience in Web, Mobile and Application Development. Focused on .Net, Java, VB, SQL Server and related technologies as both a career and a personal passion

Comments and Discussions

 
GeneralTry this http://www.codeproject.com/KB/database/Cross-Tabs-... Pin
Kannan K R14-Mar-11 23:36
Kannan K R14-Mar-11 23:36 

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.