如何将多个 oracle 行合并为一个?

How can I get several oracle rows into one?

在这种oracle情况下,如何在单个查询中得到以下结果?

Table 1
Customer | Order_Number  
1          1
1          2 
2          1 

Table 2 
Customer | Order_Number  | Employee | Tag
1            1             Bob        on hold
1            1             Larry      shipped
1            2             Larry      shipped

Results
Customer   | Order_Number   | Tags
1              1               Bob - on hold; Larry - shipped
1              2               Larry - shipped;
2              1               (Empty or null) 

我在将标签作为单个字符串返回时被绊倒了。

您需要LISTAGG

如果你的Oracle版本够旧,可以换成user-defined aggregate function, WM_CONCAT or SYS_CONNECT_BY_PATH

你没有提到你的 DB version。所以答案完全取决于你使用的是哪个版本。

如果您在 11g 及以上,请使用 LISTAGG

但是,如果您使用的是 pre 11g 版本,那么您有以下选择:

  1. ROW_NUMBER() 和 SYS_CONNECT_BY_PATH Oracle 9i 中的函数
  2. Oracle 10g 中的 COLLECT 函数
  3. STRAGG Tom Kyte 在此处建议的函数 http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2196162600402

注意:从不在生产系统中使用WM_CONCAT,它没有记录。只需向 Oracle 支持提出 SR 并说您使用过它,然后查看响应。 12c.

中不存在

此处有更多示例http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php