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?

1
  • we can use, having <condition> part. Commented Sep 24, 2021 at 11:10

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

It works with count or sum. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.