合并SQL中的多行

时间:2015-04-13 02:35:25

标签: sql

表:t1

Year    Project_Estimate    WorkStation_Code
null    159610              Totals
117825  null                Totals
null    1234                Data
3345    null                Data

预期输出

Year    Project_Estimate    WorkStation_Code
117825  159610              Totals
3345    1234                Data

请帮我解决此问题。

3 个答案:

答案 0 :(得分:1)

试试这个:

select max(Year), max(Project_Estimate), WorkStation_Code
from t1
group by WorkStation_Code

答案 1 :(得分:0)

您可以通过聚合执行此操作:

select max(Year) as year, max(Project_Estimate) as Project_Estimate,
       WorkStation_Code
from table t
group by WorkStation_Code;

答案 2 :(得分:0)

您可以简单地使用聚合函数,假设您只有两行要合并: 查询将如下所示:

 select max(Year) as year,max(Project_Estimate) as Project_Estimate,
       WorkStation_Code
 from myTable
 group by WorkStation_Code;