简体   繁体   中英

With single table, how do I collapse multiple rows with mutually exclusive column data

I wish to combine rows of a table, with mutually exclusive column values, retaining all columns: For example:

Name Data1 Data2 Data3 .... DataN
NAM1  6     NULL  NULL ...   NULL
NAM1  NULL  NULL  7    ...   NULL
NAM2  NULL   8    NULL ...   NULL
NAM3  5     NULL  NULL ...   NULL
NAM3  NULL  NULL   9   ...   NULL
NAM3  NULL   4    NULL ...   NULL
NAM4  etc

The characteristic of the data on each row of the table only 1 of the values Data1 ... DataN will not be NULL

I want the results to be

NAME DATA1 DATA2 DATA3 ... DATAN
NAM1  6     NULL  7    ...  NULL
NAM2  NULL  8     NULL ...  NULL
NAM3  5     4     9    ...  NULL
NAM4
select Name, MAX(Data1), MAX(Data2), MAX(Data3) from test
group by Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM