对于一位员工,应在一行中返回In time和Out time

时间:2018-07-03 10:45:03

标签: sql oracle over-clause

select 
    compcode, emplcode, attndate, costcode,
    decode(shiftflg, 'I', readtime) INTIME,
    decode(shiftflg, 'O', readtime) OUTTIME
from
    ecatnrec
where  
    emplcode = 'RF025'
order by 
    emplcode;

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用聚合:

select compcode, emplcode, attndate, costcode,
       max(case when shiftflg = 'I' then readtime end) as INTIME,
       max(case when shiftflg = 'O' then readtime end) as OUTTIME
from ecatnrec
where  emplcode = 'RF025'
group by compcode, emplcode, attndate, costcode
order by emplcode;

这假设group by键的唯一值的“ I”和“ O”行最多。