A
A
Andrey Strelkov2020-01-22 14:39:58
SQL
Andrey Strelkov, 2020-01-22 14:39:58

How to list values ​​using LISTAGG in a query with many JOINs from different tables?

Good afternoon, there is a table to which many other tables will be connected through a MANY TO MANY connection.
The task is to display the first field of the first table and the values ​​of the linking tables, but each in its own field, separated by a separator, i.e. how to denormalize information
Here you can clearly see the problem
sqlfiddle.com/#!4/9b70c/2
The result is the following
ID ; CPU_HASH ; MONITOR_HASH
1 ; 10, 10, 10; 5, 15, 25
And in fact I want to get such an
ID ; CPU_HASH ; MONITOR_HASH
1 ; ten ; 5, 15, 25

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Cherepakhin, 2020-01-22
@strelkov_av

SELECT report.id,
(select listagg(relation_cpu_id, ', ') WITHIN GROUP (ORDER BY id) from v_cpu_relation where report.id = v_cpu_relation.relation_report_id) AS cpu_hash,
(select listagg(relation_monitor_id, ', ') WITHIN GROUP (ORDER BY id) from v_monitor_relation where report.id = v_monitor_relation.relation_report_id) AS monitor_hash
FROM report
GROUP BY report.id

I
idShura, 2020-01-22
@idShura

So?

SELECT r.id,
       c.relation_cpu_id cpu_hash,
       LISTAGG(m.relation_monitor_id, ', ') WITHIN GROUP (ORDER BY m.id) AS monitor_hash
  FROM report r
       LEFT JOIN v_cpu_relation c ON r.id = c.relation_report_id
       LEFT JOIN v_monitor_relation m ON r.id = m.relation_report_id
 GROUP BY r.id, 
          c.relation_cpu_id

5e28452d98106444279407.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question