![](/img/trans.png)
[英]Two rows with the same id and two different values, getting the second value into another column
[英]Concatenate two rows value with same id but different column values
当同一列的第一行和第二行的值相同时。 我怎样才能合并这个?
请参阅附图。我想要突出显示的列
例如
我想得到桌子:
A/b ID SUFFIX Identify No MERGED id
CON 123 C 12345-33344441-8 123-456
CON2 456 C1 12345-33344441-8 123-456
如何合并具有相同 ID 的多个行值?
当同一列的第一行和第二行的值相同时。 我怎样才能合并这个?
例如
我想得到表:
A/b ID SUFFIX Identify No MERGED id
CON 123 C 12345-33344441-8 123-456
CON2 456 C1 12345-33344441-8 123-456
我猜您想连接所有ID
列以匹配IdentityNo
如果是这样,你可以这样做
select t.ab,
t.id,
t.suffix,
t.identifyno,
( select string_agg(convert(varchar(50), t1.id), '-')
from table1 t1
where t1.identifyno = t.identifyno
) as merged_id
from table1 t
在这个DBFiddle中自己尝试
当行的排序方式使得匹配的 IdentifyNo 彼此不匹配时,这种方法也没有问题。
假设那是你偏离路线后的样子
编辑(在 OP 更改列名之后)
新的DBFiddle
新查询
select t.id,
t.primary_Secondary,
t.id2,
t.PassportNO,
( select string_agg(convert(varchar(50), t1.id2), '-')
from table1 t1
where t1.PassportNO = t.PassportNO
) as merged_id
from table1 t
新查询的结果
ID | 小学_中学 | id2 | 护照号 | merged_id |
---|---|---|---|---|
1个 | 小学 | 242 | 12345-789 | 242-342 |
2个 | 秒 | 342 | 12345-789 | 242-342 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.