SQL one table aggrigation
Posted
by Lostdrifter
on Stack Overflow
See other posts from Stack Overflow
or by Lostdrifter
Published on 2010-03-24T20:41:28Z
Indexed on
2010/03/24
20:43 UTC
Read the original article
Hit count: 257
Ok, for the last few days I have been attempting to find a method to pull a very important set of information form a table that contains what I call daily counts. I have a table that is setup as follows.
person|company|prod1|prod2|prod3|gen_date
Each company has more then one person, and each person can have different combination of products that they have purchased. What I have been trying to figure out is a SQL statement that will list the number of people that have bought a particular product per company. So an output similar to this:
Comp ABC | 13 Prod1 | 3 Prod2 | 5 Prod 3
Comp DEF | 2 Prod1 | 15 Prod2 | 0 Prod 3
Comp HIJ | 0 Prod1 | 0 Prod2 | 7 Prod 3
Currently if a person did not select a product the value being stored is NULL.
Best I have right now is 3 different statements that can produce this information if run on there own.
SELECT Count(person) as puchases, company FROM Sales WHERE prod1 = '1' and gendate = '3/24/2010' Group BY company
© Stack Overflow or respective owner