Home > Back-end >  Querying pivot table to return a list of referred rows
Querying pivot table to return a list of referred rows

Time:05-11

I have these 6 tables and query which returns everything I want, but I want to add a new pivot table, so I can get a list of rows linked to it.

Query:

SELECT DISTINCT ON (pg.id, p.prod_id)
    pg.group_name, p.name AS prod_name, v.version,
COALESCE((select default_something from version_child where version_id = v.id),
(select default_something from product_child where prod_id = p.prod_id),
(select default_something from product_group_child where group_id = pg.id)
) as something
FROM product_group pg
LEFT JOIN product p ON pg.id = p.group_id
LEFT JOIN version v ON v.prod_id = p.prod_id
ORDER BY pg.id, p.prod_id, v.version DESC;

Product Group Table

id  group_name
---------------------------
1   Nice
2   Very Nice

Product table

prod_id  name      group_id
---------------------------
1       something     2
2       psp3          1
3       bundle1       2
4       bundle2       1

Version Table

version_id  prod_id    version
---------------------------
1           2           1.0
2           2           1.1
3           3           2.3
4           1           0.1
5           4           0.4
6           1           0.2

Product Group Child Table

pgt_child_id  group_id    default_something
---------------------------------
1             2            root2
2             1            root1      

Product Child table

pt_child_id  prod_id      default_something
-------------------------------------------
1             2            override2

Version Child Table

v_child_id  version_id    default_something
-------------------------------------------
1             3           winner

New Pivot Table

p_id.    version_id        prod_id
----------------------------------
1          3              2 
2          3              1 
3          5              1

Running the query DBFiddle I get this now:

Group_name  prod_name    version    default_something
-----------------------------------------------------
Nice        psp3          1.1       override2
Nice        bundle2       0.4       root1
Very Nice   something     0.2.      root2
Very Nice   bundle1       2.3.      winner

What I want is like this

Group_name  prod_name    version    default_something  ref
-----------------------------------------------------------
Nice        psp3          1.1       override2           []
Nice        bundle2       0.4       root1               ["something"]
Very Nice   something     0.2.      root2               []
Very Nice   bundle1       2.3.      winner              ["something", "psp3"]

the ref column can be an array or comma separated string. Basically I need to add new column ref which joins pivot table with product and version table and return an array/comma separated string. Would be really helpful to have a modified dbfiddle to do what I want it to it.

CodePudding user response:

Here's a solution to your riddle:

SELECT DISTINCT ON (pg.id, p.prod_id)
       pg.group_name, p.name AS prod_name, v.version
     , COALESCE((SELECT default_something FROM version_child WHERE version_id = v.id)
              , (SELECT default_something FROM product_child WHERE prod_id = p.prod_id)
              , (SELECT default_something FROM product_group_child WHERE group_id = pg.id)
               ) AS something
     , ARRAY(SELECT p1.name
             FROM   pivot pv
             JOIN   product p1 USING (prod_id)
             WHERE  pv.version_id = v.id
            ) AS ref
FROM   product_group pg
LEFT   JOIN product  p ON pg.id = p.group_id
LEFT   JOIN version  v ON v.prod_id = p.prod_id
ORDER  BY pg.id, p.prod_id, v.version DESC;

(Rather than an answer to a question.)

db<>fiddle here

Related:

Alternatively, you could use a LATERAL subquery:

  • Related