我有下表
name type value
a mobile 123456
a home tel 456789
a Office addr add1
a home addr add2
b mobile 456456
b home tel 123123
b Office addr add3
b home addr add4
我想做一个这样的SQL表
name phone address
a 123456; 456789 add1; add2
b 456456; 123123 add3; add4
我试过这个 SQL:
SELECT name,
LISTAGG(t.CONTACT_INFO,';')
WITHIN GROUP (ORDER BY type) AS phone where type in ('mobile', 'home tel'),
LISTAGG(t.CONTACT_INFO,';')
WITHIN GROUP (ORDER BY type) AS address where type in ('Office addr', 'home addr')
FROM table t
GROUP BY name
它不起作用。我知道是因为 WHERE 子句不是它需要的位置。但是我可以在哪里插入限制?如果我这样做
SELECT name,
LISTAGG(t.CONTACT_INFO,';')
WITHIN GROUP (ORDER BY type) AS phone,
LISTAGG(t.CONTACT_INFO,';')
WITHIN GROUP (ORDER BY type) AS address
FROM table t
GROUP BY name
它看起来像
name phone address
a 123456; 456789;add1; add2 123456; 456789;add1; add2
b 456456; 123123;add3; add4 456456; 123123;add3; add4