![](/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.