question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: support for transpiling "distinct on" (eg: postgres >> redshift)

See original GitHub issue

This 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:closed
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
barakaloncommented, Nov 23, 2022

Alright, this should just work now. No need to explicitly call the transform.

0reactions
barakaloncommented, Nov 23, 2022

Ah I think I did…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found