Tuesday, 17 September 2013

Postgres - using DISTINCT ON to get first row, but also getting the COUNT(*) work

Postgres - using DISTINCT ON to get first row, but also getting the
COUNT(*) work

I'm using
SELECT DISTINCT ON()
to return a particular row from a group of rows. Works well. But what I
also want to return is the "COUNT(*)". So it might look like
SELECT DISTINCT ON(name)
name, num_items, COUNT(name)
FROM customers
ORDER BY name, num_items DESC
But of course, I get an error saying "name should be in a GROUP BY
aggregate function".
How can I achieve the same result with the "count()" included?

No comments:

Post a Comment