简体   繁体   English

在多行中填充的SQL相同ID具有不同的列值-在一行中需要一个ID

[英]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.

相关问题 将具有相同 ID 但多列的“是”/“否”值不同的多行组合成一行,显示所有“是”/“否”值 - Combining multiple rows with the same ID, but different 'Yes'/'No' values for several columns, into one row showing all 'Yes'/'No' values SQL 更多行 ID 相同但列不同 - SQL More rows with the same ID but one different column 将具有相同ID但不同数据的多行合并为一行 - Merge multiple rows with same ID but different data into one row 将具有相同id但不同列值的两行合并为一行[mysql] - merge two rows with same id but different column values into one row [mysql] SQL在一列中获取具有相同ID但值不同的所有行 - SQL get all rows which have the same id but different values in one column SQL,一个在一列中具有不同值的id - SQL, One id with different values in one column 将具有相同ID的多个行合并为一行 - Merge multiple rows with same ID into one row 将具有相同ID的多行合并为一列 - Combine Multiple Rows with same ID into one column 我有一个 SQL 表,其中一个浮点列填充了如下值: - I have a SQL table with one float column populated with values like these: 需要一个 SQL 选择语句来返回在一个列中具有相同 id 而在另一列中具有不同值的行 - Need a SQL select statement to return rows that have the same id in one column and distinct value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM