I’m using car makes as an example, which fits my situation nicely.
Example query I have now, that gives a simple count per state:
SELECT
state as State,
count(distinct(idnumber)) as Total
FROM
database.table
WHERE
make IN('honda', 'toyota', 'subaru')
GROUP BY
state
ORDER BY
state
Note that this would give me the count for each of the car makes, excluding things like Ford, Chevy, etc. The list of makes would be every make.
Is there a way I can break that down to give me the count of each make by state without resorting to a sub-query? In my head it would be like having a where statement in the count(distinct(idnumber)) select, but I’m not sure that’s possible.
Here’s what is in my head:
SELECT
state as State,
count(distinct(idnumber)) as Total_State,
(count(distinct(idnumber)) WHERE make = 'honda') as Total_Honda
WHERE
make IN('honda', 'toyota', 'subaru')
GROUP BY
state
ORDER BY
state
You could add multiple columns to your group by:
I may misunderstand your question, but you can group along two columns, so you will get the number of fords made in CA and hondas made in CA etc
To be explicit, your query would be this:
Just as a fun test, I did this:
And got back:
Which is what I expected. Is that what you were thinking?