Baz
Baz

Reputation: 13135

Time based data

Lets say I have a table called orders, where each row stores an id, a product Id, a sold date, and the number of this product sold for the order in question, as follows:

0 101 2011-12-12 2 
1 101 2011-12-12 1
2 101 2011-12-12 1
3 101 2011-12-12 1
4 101 2011-12-11 2
5 101 2011-12-11 1
6 101 2011-12-10 1
7 232 2011-12-10 5
8 101 2011-12-10 1
...

How can I query this table so as to get the number of each product sold per day, as follows:

101 2011-12-12 5 
232 2011-12-12 0
101 2011-12-11 3
232 2011-12-11 0
101 2011-12-10 2
232 2011-12-10 5

And what would one do if the data in question was to be grouped by weeks, months, or years?

Upvotes: 0

Views: 44

Answers (1)

taylonr
taylonr

Reputation: 10790

This should get you most of the way there:

select productId, date, sum(qty) as total
from orders
group by productId, date
order by productId

Upvotes: 3

Related Questions