PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Serve GROUP BY con COUNT(*) in SQL?

COUNT è funzione aggregata, quindi devi dire su quale campo aggregare con GROUP BY:

SELECT column_a, COUNT(*)
FROM my_schema.my_table
GROUP BY column_a;

Solo SELECT COUNT(*) restituisce totale righe, come "group by nothing". * conta tutto, inclusi NULL. Per condizione specifica usa COUNT(expression).