0

I have a query which in I joined twice the same table, i.e:

left join tag tag1 [...]
left join tag tag2 [...]

I would like to have query with one column called tag which in I will have unique values from both tables (tag1, tag2).

Example:
For now I am able to select unique values from each one column with jsonb_agg(distinct tag_name) and in my query I have two columns:

tag1            | tag2
[Sport, Music]  | [Sport, Games]

And I would like to have one column with unique values from tag1 and tag2 like:

tag
[Sport, Music, Games]
Tomasz
  • 89
  • 1
  • 1
  • 6

1 Answers1

0

If tag1 and tag2 are two tables with the same column tag_name, then you can use UNION in order to merge the values from both tables while avoiding duplicates :

SELECT jsonb_agg(r.tag_name)
FROM
(
SELECT tag_name
  FROM tag1
UNION
SELECT tag_name
  FROM tag2
) AS r
Edouard
  • 6,577
  • 1
  • 9
  • 20