[英]SQL Same ID Populated in Multiple Rows with Different Column Values Populated--Need One ID in One Row
I have a table like following: 我有一个如下表:
ID Col1 Col2 Col3 Col4
001 A
001 B
001 C
001 D
002 X
002 Y
I want the result like the following: 我想要如下结果:
ID Col1 Col2 Col3 Col4
001 A B C D
002 X Y
The challenge is the number of columns is unknown, maybe this it has a Col5 or even Col10. 挑战是未知的列数,也许它具有Col5甚至Col10。 Any thoughts?
有什么想法吗? Much appreciated.
非常感激。
You can do this with aggregation: 您可以通过聚合来做到这一点:
select id, max(col1) as col1, max(col2) as col2, max(col3) as col3, max(col4) as col4
from t
group by id;
This assumes that there are no duplicates within a column for an id. 假设ID的列中没有重复项。
For additional columns, you would need to add additional clauses to the select
statement. 对于其他列,您需要在
select
语句中添加其他子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.