查询数据透视表以返回引用的行列表



我有这6个表和查询,返回我想要的一切,但我想添加一个新的数据透视表,所以我可以得到一个行链接到它的列表。

查询:

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;

产品分组表

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

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

版本表

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

产品组子表

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

Product子表

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

版本子表

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

新建数据透视表

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,它与产品和版本表连接数据透视表,并返回一个数组/逗号分隔的字符串。如果有一个修改过的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<此处小提琴>

相关:

  • 为什么array_agg()比非聚合ARRAY()构造函数慢?

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

  • 在PostgreSQL中,LATERAL JOIN和子查询有什么区别?

相关内容

  • 没有找到相关文章

最新更新