Skip to content
Advertisement

SQL – Postgres string agg is giving duplicates

Im trying to collect the foreign key mapping from system tables. And I used this below query.

query 1:

But this won’t provide the Primary tables columns that are pointing as Fk. So I found this query on Stackoverflow.

query 2

Now this query gives individual columns. Lets say there is a FK like (logid, item) then I’ll get two rows. I thought to use string_agg with my first query, I got the results but it endup duplicates. Like(logid, item,logid, item)

query with duplicates:

Can someone help me to fix this query?

Example FK:

Expected Query output:

enter image description here

Advertisement

Answer

Use SELECT DISTINCT... to remove duplicates

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