Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Using Pivot In SQL for Reporting

0.00/5 (No votes)
15 Mar 2011CPOL 7.1K  
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)