Re:Distinct on: a confusing, unique and useful feature in Po...
by zeroimpl on Friday March 20, 2020 @09:45PM
(#22643532)
I’ve created a custom aggregate in Postgres that lets you essentially get the same result as what MySQL does:
As it’s name implies, returns the last value that the aggregate encountered for the group. You can add an “order by” within the parenthesis to make it deterministic.
[ Parent
| on HN ]
Re:Distinct on: a confusing, unique and useful feature in Po...
by profquail on Saturday March 21, 2020 @10:07AM
(#22646561)
Isn’t this what the LAST_VALUE window function does? If so, that’s a standardized SQL syntax which is supported in both postgresql and recent versions of MySQL.
https://www.postgresqltutorial.com/postgresql-last_value-fun...
Re:Distinct on: a confusing, unique and useful feature in Po...
by zeroimpl on Saturday March 21, 2020 @08:50PM
(#22651643)
kinda. You can use aggregates as window functions, but cannot use window functions as aggregates.
So it would be:
select a, last_value(b) over(partition by a), last_value(c) over(partition by a) from table
Note the lack of a "GROUP BY" so this will result in duplicates unless you add a DISTINCT.