How to use a COUNT with condition?

Asked

Viewed 701 times

3

I have a table quarto, wanted to return the amount of rooms you have on each floor (the room has an attribute andar), but I don’t know how to get him back from every floor

SELECT andar, COUNT(* WHERE andar = andar????? ) AS quantidade

I wanted a comeback like

andar  quantidade
1      5
2      10
3      6

1 answer

3


You want the function COUNT() either aggregator, or count all elements found within a data group, that group is defined by the attribute andar, then use the clause GROUP BY.

SELECT andar, COUNT(*) AS quantidade FROM andares GROUP BY andar

Behold working in the SQL Fiddle. Also put on the Github for future reference.

  • That’s it! Thanks! I’m learning and I was in doubt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.