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

查看:21
本文介绍了如何通过逗号分隔将 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 : 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天全站免登陆