1

I have a table of FinishedGames with columns: category and a score. I need to know how many games by category finished with more than certain score, but I don't understand the count if structure in PostgreSQL.

select category, count(score) as rounds, count(if score > 7) as wins
from "FinishedGames" group by category;

Does anyone knows how to do this in PostgreSQL?

Camilo Ortegón
  • 3,414
  • 3
  • 26
  • 34

1 Answers1

8

you should use the CASE clause within the count function. e.g. COUNT(CASE WHEN score > 7 THEN 1 ELSE NULL END) (or SUM with 1 and 0 - same result...)

Regards, Jony

cohenjo
  • 576
  • 3
  • 5