简体   繁体   English

T-SQL如何将多行合并为单行

[英]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. 每个成员代码的price列值始终相同,每个成员代码只有一个price%值。

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 我的表包含60000条记录,所以我不能做

select max(column) where member_code = x

The sql fiddle http://sqlfiddle.com/#!6/7f084/1 sql小提琴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 在价格列上具有MAX汇总函数的GROUP BY Member_Code将有所帮助

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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