有没有一种方法可以使用两个表将多个行行连接为单个行,而在ORACLE中这些表的值由逗号分隔?
例子:
IdN Name --------- 1 A 2 B 3 C
IdC Car ------------ 1 Ferrari 1 BMW 2 SEAT 2 FIAT 3 FORD
结果为:
A Ferrari,BMW B SEAT,FIAT C FORD
我想知道是否有这样的事情:
SELECT NAME,CAR FROM TABLE1, TABLE2 where TABLE1.IdN=TABLE2.IdC
这将返回类似:
A FERRARI A BMW B SEAT B FIAT C FORD
有没有一种简单的方法可以用逗号分隔值将其“连接”成一行?
看看LISTAGG
http://www.oracle- base.com/articles/misc/StringAggregationTechniques.php
就像是:
SELECT NAME, LISTAGG(CAR, ',') WITHIN GROUP (ORDER BY CAR) AS CARS FROM (SELECT NAME,CAR FROM TABLE1, TABLE2 where TABLE1.IdN=TABLE2.IdC) GROUP BY NAME;