COUNT, DISTINCT and SUM all in one

COUNT, DISTINCT and SUM all in one

am 23.05.2006 16:06:30 von Peter Lewis

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

Re: COUNT, DISTINCT and SUM all in one

am 23.05.2006 17:07:30 von zac.carey

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.

Re: COUNT, DISTINCT and SUM all in one

am 04.06.2006 14:03:38 von Peter Lewis

"strawberry" wrote in message
news:1148396850.646974.202020@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