I would like to get something like this:
*Customer *2009 *2010 *
|------------|---------|--------|
|Peter |120 |240 |
|Johe |455 |550 |
My first approach to the query was this:
Select c.name, sum(o2009.price), sum(o2010.price) from customer c
join orders o2009 on (o2009.customerId = c.id AND o2009.year = 2009)
join orders o2010 on (o2010.customerId = c.id AND o2010.year = 2010)
group by c.id
Unfortunately this is completely wrong. I guess I could run 2 queries and then build a union, but maybe there is something simpler?
Just modifying one of the answers to get customers with no orders –