将连续的两行合并为一列
假设,我有一张像这样的表:
| 员工代码 | 条目类型 | 时间 |
|---|---|---|
| ABC200413 | 在 | 8:48AM |
| ABC200413 | 出去 | 下午 4:09 |
| ABC200413 | 在 | 下午 4:45 |
| ABC200413 | 出去 | 下午 6:09 |
| ABC200413 | 在 | 晚上 7:45 |
| ABC200413 | 出去 | 晚上 10:09 |
回答
提供mytable仅包含有效的输入/输出事件对
select EmployeeCode,
max(case EntryType when 'IN' then TIME end ) IN_TIME,
max(case EntryType when 'OUT' then TIME end ) OUT_TIME
from (
select EmployeeCode, EntryType, TIME,
row_number() over(partition by EmployeeCode, EntryType order by TIME) rn
from mytable
)t
group by EmployeeCode, rn
order by EmployeeCode, rn
否则,首先需要进行某种清理。