Skip to content
Advertisement

Find IDs with only one observation in PostgreSQL

I have the following table:

And I would like to filter out all visitor_id’s that have only one observation (or record). In this case VIS03 and VIS04, so I must end up with VIS01 and VIS02. I tried this:

The expected result should be:

But I guess that something like a rank is needed. Any help will be greatly appreciated.

Advertisement

Answer

There are probably other ways of doing this, but it you create a derived table CTE of only the visitor_ids that have more than 1 row, then use that in the join to your table. Obviously, if my_table is large an index would enhance the performance.

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