Feature request: support for transpiling "distinct on" (eg: postgres >> redshift)
See original GitHub issueThis is a useful PostgreSQL construct that Redshift (sadly!) still does not have support for; for example, the following would return the most recent single row per id-group (as determined by the some_timestamp
sort order).
-- PostgreSQL
SELECT DISTINCT ON (some_id) AS some_id, some_value, some_other_data
FROM some_table
ORDER BY some_timestamp DESC
Rewriting for Redshift (and/or any other backend that does not support “DISTINCT ON”) would typically be done as a select
from a nested subquery that uses the row_number
window function:
-- Redshift
SELECT some_id, some_value, some_other_data
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY some_id ORDER BY some_timestamp DESC) as r, * FROM some_table
)
WHERE r = 1
(I imagine this one isn’t as straightforward as a function-rewrite, but it’s a query pattern I find myself coming across quite frequently, so good to have 😉
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
SELECT DISTINCT examples - Amazon Redshift
Provides examples of how to use SELECT DISTINCT.
Read more >Fixing DISTINCT ON Not Supported
1 SELECT DISTINCT ON is not supported. The DISTINCT ON clause is a Postgres function which selects the first value for a group...
Read more >Create Upsert Yourself for Amazon Redshift Databases
Let's look at how to create our own version of an UPSERT SQL statement in a PostgreSQL-based Amazon Redshift database, updating data that ......
Read more >How Redshift differs from PostgreSQL - Stitch Data
INSERT, UPDATE, and DELETE Redshift doesn't support the WITH clause. VACUUM The parameters for VACUUM are different between the two databases. For example,...
Read more >Redshift Pitfalls And How To Avoid Them - Heap
Amazon Redshift is a data warehouse that's orders of magnitudes cheaper than ... a lot about Redshift and how it's different from Postgres....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alright, this should just work now. No need to explicitly call the transform.
Ah I think I did…