I am trying to get around an apparently simple problem in MySQL: I have a table composed by 3 fields and I would like to form a query that returns a field selected as DISTINCT and two more columns which return a COUNT of the remaining two fields when a specific condition is met.
To illustrate (columns named first, second and third left to right):
| VzokAAD | aaa | 0 |
| VziAAAT | bbb | 0 |
| VziAAAT | ccc | 1 |
| VziAAAT | ddd | 0 |
| W0cZAAT | eee | 1 |
| VziNAAT | fff | 1 |
| VzpqAAD | ggg | 1 |
| VzpqAAD | hhh | 1 |
My current query is structured as follows:
SELECT DISTINCT(first) AS field_one, COUNT(second)
FROM table WHERE third = 0 GROUP BY field_one;
So the above query will return:
| VzokAAD | 1 |
| VziAAAT | 2 |
I can change the query to something like this:
SELECT DISTINCT(first) AS field_one, COUNT(second)
FROM table WHERE third = 1 GROUP BY field_one;
And it will return:
| VzpqAAD | 2 |
| VziNAAT | 1 |
| W0cZAAT | 1 |
| VziAAAT | 1 |
How do I get both queries combined together so that, I get the first column GROUPED and DISTINCT and two additional columns with the COUNT of third =1 and third =0 respectively.
Basically something like this (wrong of course):
SELECT DISTINCT(first) AS field_one, COUNT(second WHERE third = 1)
AS alpha, COUNT(second WHERE third = 0) AS beta FROM table GROUP BY field_one;
I tried to use CASE and IF control flow functions however I only managed to get the results defined in a single column.
Thanks to anyone willing to lend a hand!