0

I have a table that looks like:

field1 | field2
   1   | ready
   2   | ready
   1   | in_progress
   1   | ready
   2   | in_progress

How can i count number of "ready" fields for each field1? For this example answer must be:

field1 | field2
   1   |   2
   2   |   1
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76

2 Answers2

1

Simple filter and aggregation will do:

SELECT field1,
    COUNT(*) AS field2
FROM your_table
WHERE field2 = 'ready'
GROUP BY field1;

If you want to get those field1 values where count can be 0:

SELECT field1,
    SUM(field2 = 'ready') as field2
FROM your_table
GROUP BY field1;

SUM(field2 = 'ready') uses the fact that MySQL treats true as 1 and false as 0.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0

This can be done using simple aggregation using the count() function:

select field1, count(field2) as field2
from yourtable
where field2 = 'ready'
group by field1

By filtering your data using a WHERE = 'ready' you then count the number of rows for each value in field1.

ollie
  • 1,009
  • 1
  • 8
  • 11