【问题标题】:DB2 SQL : How to get 2 rows data into 1DB2 SQL:如何将 2 行数据放入 1
【发布时间】:2021-02-10 10:59:32
【问题描述】:

我从源中获取员工数据,这为同一员工提供了 2 行。第一行有工资,第二行有佣金。要确定是工资还是佣金,我有一个 Flag 列。

现在我想将它存储在我的目标表中的单行中,在那里我将薪水和佣金作为列。

【问题讨论】:

  • 样本数据和期望的结果会有所帮助。所有员工都正好有两行吗?还有其他列吗?

标签: sql db2 db2-zos


【解决方案1】:

使用条件聚合

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

【讨论】:

    【解决方案2】:

    尝试一下,也许会奏效

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-07
      • 2023-01-30
      • 1970-01-01
      • 2018-11-12
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      相关资源
      最近更新 更多