is there any way to implement top 1 result from each group without using ROW_NUMBER?
table_a below has already 1 billion rows, so I want to use the most efficient way.
And the below query is what I’m using.
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY column_a, column_b, column_c ORDER BY column_d) AS row_num,
*
FROM table_a
)
WHERE row_num = 1
2
Answers
This can be done using
group by
and the aggregate functionmin()
:An alternative would by
distinct on
. Howeverdistinct on
is a Postgres extension and not a SQL standard.