DB2 SQL:如何将2行数据放入1

问题描述

我从源头获取员工数据,该数据为同一员工提供了2行。第一排有薪水,第二排有佣金。为了确定是薪水还是佣金,我只有一个“标志”列。

现在我想将其存储在目标表的单行中,在该表中我将薪水和酬劳作为列。

解决方法

使用条件聚合

select employee_id,max(case when flag=0 then salary end) as salary,max(case when flag=1 then commission end) as commission
from tablename
group by employee_id
,

尝试一下可能会起作用

select employee_id,sum(salary)salary,sum(commission) from
 (select employee_id,0 as salary,commission from tblname where flag=1
    union all
  select  employee_id,salary,0 commission from tblname where flag=0
  )a
group by employee_id