I have the problem that I want to join several tables to create a new view. The view is working fine so far but I struggle with the following problem:
I have a table name it 'MyTable1'. In this table there is an ID. I have a second table name it 'MyTable2'. This table is referenced to 'MyTable1' with the ID column.
MyTable1:
ID
567
MyTable2:
ID MyTable1_ID object_ID
1 567 896
2 567 967
3 567 756
Code:
SELECT
MyTable1.ID,
MyTable2.ID,
MyTable2.object_ID
FROM
MyTable1
LEFT JOIN MyTable2 ON MyTable2.MyTable1_ID = MyTable1.ID
Output:
MyTable1.ID MyTable2.ID MyTable2.object_ID
567 1 896
567 2 967
567 3 756
Desired Output:
MyTable1.ID MyTable2.ID MyTable2.object_ID
567 1;2;3 896;967;756
I have tried with LISTAGG
but also get only 3 rows as output:
SELECT
MyTable1.ID,
MyTable2.ID,
MyTable2.object_ID
CASE WHEN (SELECT count(*) FROM MyTable2 JOIN MyTable1 ON MyTable2.MyTable1_ID = MyTable1.ID having count(*) > 1) > 1 THEN LISTAGG(MyTable2.object_id, '; ') WITHIN GROUP (order by MyTable2.object_id) ELSE MyTable2.object_id END
FROM
MyTable1
LEFT JOIN MyTable2 ON MyTable2.MyTable1_ID = MyTable1.ID
Can anybody help me?
Thanks