Skip to content
Advertisement

Select Last Rows with Distinct Field

I have a table with the following schema:

In this table only the id field is unique. I’m concerned with getting the rows containing the last X distinct itemid, ordered by date.

For example, in the sample above, if I’d like to get the last 3 distinct itemid, I’d be getting the first 4 rows, since in the first 4 rows we have three distinct itemid: 1000, 1002 and 1001. I’m not sure how to achieve this using a single SQL statement.

Advertisement

Answer

If I understand correctly, you would like to count the number of distinct item ids up to each each row (by date) and return all rows where the count is three.

If Postgres supported this, you could use:

Alas, Postgres does not support COUNT(DISTINCT) as a window function. But you can calculate it using DENSE_RANK():

However, this returns all the most recent rows up before the 4th item — so it has extra rows.

To get four rows, you want the first where the item id is “3”. One method is:

You can also do this by identifying the first occurrence of the “third item” and then choosing all rows up to that row:

This fiddle shows each of these.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement