如何将2行合并成一个逗号分隔? [英] How to Merge 2 Rows into one by comma separate?

查看:185
本文介绍了如何将2行合并成一个逗号分隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将这些单独的行合并到一个列,现在如何以逗号分隔合并列,

I need to merge this individual rows to one column, I now how to merge column by comma separated,

+---------------+-------+-------+
|CID            |Flag   |Value  |
+---------------+-------+-------+
|1              |F      |10     |
|1              |N      |20     |
|2              |F      |12     |
|2              |N      |23     |
|2              |F      |14     |
|3              |N      |21     |
|3              |N      |22     |
+---------------+-------+-------+

希望的结果可以是任何结果,

Desired Result can be anything,

+-----------+----------------------------+    +--------------------------+
|Part Number| Value                      |    | Value                    |   
+-----------+----------------------------+    +--------------------------+
| 1         | 1|F|10 ; 1|N|20            | Or | 1|F|10 ; 1|N|20          |
| 2         | 2|F|12 ; 2|N|23 ; 2|F|14   |    | 2|F|12 ; 2|N|23 ; 2|F|14 |
| 3         | 3|N|21 ; 3|N|22            |    | 3|N|21 ; 3|N|22          |
+-----------+----------------------------+    +--------------------------+

注意


小范例的正确方向的任何提示都足够了

Any hint in right direction with small example is more than enough

EDIT:
在表中有大量数据,包括父级和子级关系存在的数千个记录。我必须通过逗号分隔值转储这些文本文件在单行作为记录。认为作为主记录与这么多其他表的关系,那么所有这个记录必须打印为大线。

EDIT : I have massive data in tables like thousands of records where parent's and child relationship is present. I have to dump this into text files by comma separated values In single line as record. Think as primary record has relationship with so many other table then all this record has to be printed as a big line.

我试图通过创建查询来实现,所以负载可以分布在数据库上,只有我在业务中担心的只是将逻辑转储到文本文件或任何形式我们需要在将来。

And I am trying to achieve by creating query so load can be distributed on database and only thing i have to worry about in business is just dumping logic into text files or whatever form we need in future.

推荐答案

您可以尝试使用 LISTAGG ,您的查询将如下所示:

You can try to use LISTAGG and your query will look like this:

select a.cid, a.cid || listagg(a.flag || '|' || a.value, ',') 
from foo.dat a
group by a.cid

您可以使用不同的分隔符,当然还有如何格式化结果。

You can use different separators and of course play with how the result will be formatted.

这篇关于如何将2行合并成一个逗号分隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆