I've got an SQL query that I just can't get to display the way I want it to. Basically, we've got a table full of transactions and I'd like to get a sum (of a particular type of transaction ) purchased from February of this year until now. Here's the query for that:
select sum(amount) as "2011" from transactions
where transaction_type = 'registration'
and date_entered > '2011-02-01'
and date_entered < GetDate()
Now, I'd also like to see a sum of those same transactions but for the previous year:
select sum(amount) as "2010" from transactions
where transaction_type = 'registration'
and date_entered > '2010-02-01'
and date_entered < DateAdd(yy, -1, GetDate())
What I can't seem to figure out is how to get these sums side-by-side. I've tried UNION all
but those display in separate rows, not columns.
select sum(amount) as "2011" from transactions
where transaction_type = 'registration'
and date_entered > '2011-02-01'
and date_entered < GetDate()
UNION all
select sum(amount) as "2010" from transactions
where transaction_type = 'registration'
and date_entered > '2010-02-01'
and date_entered < DateAdd(yy, -1, GetDate())
I've also read here on Stack Overflow that PIVOT
might be an option but I've yet to see an example that I could manipulate/tweak for the queries above.
Any suggestions for how I can get this data side-by-side? I'm sure I'm overlooking something simple.
Many thanks!