Skip to content
Advertisement

See if value exists in any column of every row in PostgresQL

Imagining I have this table, I want to be able to find the brands that exist associated with every id. Note that this is not equivalent to finding if the value exists for every row, since a single id may exist several times. The result I would expect in this case would be Mercedes-Benz.

I have tried using EXIST, but I haven’t been able to find an elegant “postgresql” way to iterate in the id columns.

Result:

Advertisement

Answer

Unpivot the data — I recommend using a lateral join — and then aggregate:

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