Search code examples
sqldb2db2-zos

DB2 SQL : How to get 2 rows data into 1


I am getting employee data from source which gives me 2 rows for same employee. 1st row has a salary and 2nd has commission. to identify if it is salary or commission I have got one Flag column.

Now I want to store it in single row in my target table, where I would have salary and commision as columns.


Solution

  • try a chance maybe work

    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