PDA

View Full Version : Sql count()



jesuisbenjamin
March 6th, 2011, 07:21 AM
Hello there,

i am working on a database for a survey i am making at the moment.

I have a column "Block" which can have a value of 1 to 21. If i want to count how many entries have the value "1" in "Block" then i use COUNT() no problem.
What i'd like to achieve however is to count how many entries there are for each possible value in "Block". In other words how many entries have Block=1, how many Block=2 etc... till Block=21.
I played around thinking JOIN would help, but SQL gets confused as well as with ALIAS.
For instance it goes fine as far as this is queried ...

SELECT "Block" AS "Block1", "Block" AS "Block2" FROM "interviews" WHERE "Block1"=1 OR "Block2"=2
... but gets messy when querying:

SELECT COUNT("Block") AS "Block1", COUNT("Block") AS "Block2" FROM "interviews" WHERE "Block1"=1 OR "Block2"=2

Any idea how to do that?

Thanks.
Benjamin

AlexC_
March 6th, 2011, 10:34 AM
SELECT foo, COUNT(foo) FROM bar GROUP BY foo