简体   繁体   中英

T-SQL How to combine multiple rows into a single row

I've googled pretty much everything I can think of and haven't been able to find a solution that fits my needs. It's pretty simple in theory: I need to combine multiple rows of a result set by their member code. The values for the price columns will always be identical per member code and there will only be one price% value per member code.

Table

|Member_Code|price 1|price 1%|price 2|price 2%|
|      eeeee|     15|      10|     20|       0|
|      eeeee|     15|       0|     20|      50|

Result I need

|Member_Code|price 1|price 1%|price 2|price 2%|
|      eeeee|     15|      10|     20|      50|

My table contains 60000 records so I can't just do

select max(column) where member_code = x

The sql fiddle http://sqlfiddle.com/#!6/7f084/1

Please can someone point me in the right direction.

Thanks

GROUP BY Member_Code with MAX aggregate function on Price columns will help

select 
[Member_Code], 
max([Price List 1]),
max([Price List 1%]),
max([Price List 2]), 
max([Price List 2%]), 
max([Price List 3]),
max([Price List 3%])
from Table1 
group by [Member_Code]

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