.

SQL pivot explained

SQL pivot command:

select * from (select total, cast([date] as date)  as [date],
payment from [sales receipts]) as t
pivot
(
sum(total)
for [payment] in ([VISA], [AmEx], [MasterCard], Cash)
) as p
order by [date]

and what it pretty much does behind the scenes:

select cast([date] as date) as [date],
count(case when payment = 'VISA' THEN 1 END) as VISA,
count(case when payment = 'AmEx' THEN 1 END) as AmEx,
count(case when payment = 'MasterCard' THEN 1 END) as MasterCard,
count(case when payment = 'Cash' THEN 1 END) as Cash
from [sales receipts]
group by cast([date] as date)
order by date

What's your thoughts on this?

*

Protected by WP Anti Spam