我有2个表,其中一个表通过id
引用第一个表 例如,第一个表是具有字段的客户 id firstname lastname
------ --------- ---------
1 john smith
2 jessica james
第二个表例如是具有字段
的产品 id customer_id product descr
------- ----------- --------- ------
1 1 ts Shirt
2 1 ti Tie
3 2 sk skrit
我需要一个输出以下内容的查询
customer.firstname customer.lastname product_and_desc
------------------ ------------------ ---------------------
john smith ts-Shirt , ti-Tie
jessica james sk-skirt
为每个客户提供产品行变量。
我感谢你的帮助:)
感谢,
答案 0 :(得分:1)
您可以使用list_agg()
。在你的情况下:
select c.firstname, c.lastname,
list_agg(p.product||'-'||p.desc, ' , ') within group (order by p.id) as product_and_desc
from customer c join
product p
on c.id = p.customer_id
group by c.firstname, c.lastname;
我建议,list_agg()
的第二个论点是','而不是','。逗号前面的空格看起来有点不寻常。
答案 1 :(得分:1)
select first_name,last_name,wm_concat(product||'-'||descr) as product_and_descr
from tbl1 ,tbl2 where tbl1.id=tbl2.customer_id
group by first_name,last_name;