我有两张桌子
category -------- id product seq_num cat12 prod12 0 cat23 prod12 1 cat34 prod12 2 prod ----- sku prod seq_num sku123 prod12 0 sku234 prod12 1 sku345 prod12 2
我正在尝试使用LISTAGG函数并输出如下输出:
output ------ skuid prod catids sku123 prod12 cat12,cat23,cat34 sku234 prod12 cat12,cat23,cat34 sku345 prod12 cat12,cat23,cat34
当我单独使用类别表时,我可以使用LISTAGG并正确汇总输出(无需skuid)
使用以下查询:
SELECT product,LISTAGG(id, ',') WITHIN GROUP (ORDER BY id) AS catids FROM category GROUP BY product;
但我无法将此与prod表连接并获得所需的输出。 请帮忙。
答案 0 :(得分:2)
您可以在加入后执行此操作:
SELECT s.sku,t.product,
LISTAGG(t.id, ',') WITHIN GROUP (ORDER BY id) AS catids
FROM category t
INNER JOIN prod s
ON(t.product = s.prod)
GROUP BY t.product,s.sku;
答案 1 :(得分:0)
您可以按如下方式编写查询:
SELECT p.sku "skuid", c.product "prod",
LISTAGG(c.id, ',') WITHIN GROUP (ORDER BY id)
OVER (PARTITION BY c.product) AS catids
FROM category c
INNER JOIN prod p
ON c.product = p.prod
ORDER BY "skuid", c.product;
你也可以检查一下; it is more complicated version of usage together of listagg and join functions