查询数据透视 table 到 return 引用行的列表

Querying pivot table to return a list of referred rows

我有这 6 个 table 并查询哪个 return 是我想要的所有内容,但我想添加一个新的数据透视表 table,这样我可以获得一个行列表链接到它。

查询:

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;

产品组Table

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

产品table

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

版本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

子产品组 Table

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

产品子项table

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

子版本Table

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

新枢轴Table

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

运行 查询 DBFiddle 我现在明白了:

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

我想要的是这样的

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"]

ref 列可以是数组或逗号分隔的字符串。基本上,我需要添加新列 ref,它将枢轴 table 与产品和版本 table 和 return 和 array/comma 分隔的字符串连接起来。有一个修改过的 dbfiddle 来做我想做的事真的很有帮助。

这是您的谜语的答案:

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;

(而不是对 问题 的回答。)

db<>fiddle here

相关:

或者,您可以使用 LATERAL 子查询: