Tuesday, 20 August 2013

Can this be done in a SQL query?

Can this be done in a SQL query?

I've got a table structure like this:
month (datetime)
account (int)
product (int)
amountPaid (int)
And some example data:
month account product amountPaid
1-1-2012 1 1 50
2-1-2012 1 1 50
2-1-2012 2 1 150
2-1-2012 2 2 100

What I'd like is a query that can tell me for each month, the number of
accounts that paid for only product 1, the number of accounts that paid
for only product 2, and the number of accounts that paid for both products
1 and 2. Also, the products that each account pays for can change month to
month. For example, one month, an account might pay for only product 1,
the next month, both products 1 and 2, and the following month, only
product 2.
Can this be done in a SQL query?
The result set might like something like:
month product count
1-1-2012 1 10
1-1-2012 2 5
1-1-2012 1+2 3
2-1-2012 1 8
2-1-2012 2 4
2-1-2012 1+2 2

No comments:

Post a Comment