从2个diff列对oracle表中的数据进行分组

时间:2011-03-23 01:15:26

标签: sql oracle string-aggregation

  

可能重复:
  comma-separated list as a result of select statement in Oracle

我在SQL表中有这样的字段:

ELEMENT_CODE  DIAG_CODE
------------------------
FTD           1234
FTD           1243
FTD           6384
FTD           9938
FLA           9292
FLA           3654
FLA           1122

我需要在oracle数据库的报告中记录这样的记录:

ELEMENT_CODE  DIAG_CODE
------------------------
FTD           1234-1243-6374-9938
FLA           9292-3654-1122

1 个答案:

答案 0 :(得分:3)

对于Oracle 11g第2版:

SELECT ELEMENT_CODE, LISTAGG(DIAG_CODE, '-') WITHIN GROUP (ORDER BY DIAG_CODE) AS "DIAG_CODES"
FROM SomeTable
GROUP BY ELEMENT_CODE;

Pre Oracle 11g第2版将是:

SELECT ELEMENT_CODE, WM_CONCAT(DIAG_CODE, '-')
FROM SomeTable
GROUP BY ELEMENT_CODE;

来源:http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php