Amil Osmanli
Amil Osmanli

Reputation: 135

Selecting rows that doesn't have duplicates

Let's say I have the following table:

| sku | id | value | count |
|-----|----|-------|-------|
| A   | 1  | 1     | 2     |
| A   | 1  | 2     | 2     |
| A   | 3  | 3     | 3     |

I want to select rows that don't have the same count for the same id. So my desired outcome is:

| sku | id | value | count |
|-----|----|-------|-------|
| A   | 3  | 3     | 3     |

I need something that works with Postgres 10

Upvotes: 0

Views: 39

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

A simple method is window functions:

select t.*
from (select t.*, count(*) over (partition by sku, id) as cnt
      from t
     ) t
where cnt = 1;

This assumes you really mean the sku/id combination.

Upvotes: 2

Related Questions