Need some sort of "conditional grouping" in MySQL.

Posted by serg555 on Stack Overflow See other posts from Stack Overflow or by serg555
Published on 2010-06-02T00:48:04Z Indexed on 2010/06/02 0:53 UTC
Read the original article Hit count: 263

Filed under:
|
|

I have Article table:

id | type | date
-----------------------
 1 | A    | 2010-01-01
 2 | A    | 2010-01-01
 3 | B    | 2010-01-01

Field type can be A, B or C.

I need to run a report that would return how many articles of each type there is per every day, like this:

date       | count(type="A") | count(type="B") | count(type="C")
-----------------------------------------------------
2010-01-01 | 2               | 1               | 0
2010-01-02 | 5               | 6               | 7

Currently I am running 3 queries for every type and then manually merging the results

select date, count(id) from article where type="A" group by date

Is it possible to do this in one query? (in pure sql, no stored procedures or anything like that).

Thanks

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql