COUNT, DISTINCT and SUM all in one

Hi all (again)

More I get into MySQL and the more I read, the more I get confessed

I am trying to run a statement that does the following

Return a DISTINCT count of the number of records between two timestamps

SELECT count(DISTINCT dnote) as Total_Orders FROM $table WHERE (timestamp
BETWEEN 1146438000 AND 1148338800)

this works and brings back a result which I need

Now I need to say out of those returned records, how many of them had a
brand = 'X' and brand = 'Y' and brand = 'Z'

e.g.

Total_Orders = 900
Total where brand is X = 300
Total where brand is Y = 400
Total where brand is Z = 200

X+Y+Z should = Total_Orders

What I can't work out is how to say only SUM up the X,Y Z where a brand =
X,Y,Z


Thanks


Brian
Peter Lewis [ Di, 23 Mai 2006 16:06 ] [ ID #1328180 ]

Re: COUNT, DISTINCT and SUM all in one

SELECT brand,SUM(dnote) FROM $table GROUP BY brand;

You could probably combine this with the above into a single query.
Otherwise do a "SELECT CREATE VIEW" on your first table.
zac.carey [ Di, 23 Mai 2006 17:07 ] [ ID #1328181 ]

Re: COUNT, DISTINCT and SUM all in one

"strawberry" <zac.carey [at] gmail.com> wrote in message
news:1148396850.646974.202020 [at] 38g2000cwa.googlegroups.com...
> SELECT brand,SUM(dnote) FROM $table GROUP BY brand;
>
> You could probably combine this with the above into a single query.
> Otherwise do a "SELECT CREATE VIEW" on your first table.


Thnaks for that :)

Brian
Peter Lewis [ So, 04 Juni 2006 14:03 ] [ ID #1341572 ]
PHP » alt.php.sql » COUNT, DISTINCT and SUM all in one

Vorheriges Thema: MySQL hosting plans.
Nächstes Thema: isnull wrong number of arg error