答案 0 :(得分:0)
由于您只需要out列中的一列,并且状态1始终处于状态,状态2始终处于退出状态:
;with tmpIn as (select * from <table> where attend_state = 'c/in'),
tmpOut as (select * from <table> where attend_state = 'c/out')
select emp_CA_no, attend_date attend_date1,
(select top 1 attend_date from tmpOut where attend_date > tmpIn.attend_date and emp_AC_no = tmpIn.emp_AC_no) attend_date2,
'c/in', 'c/out'
from tmpIn
请注意,我没有将日期和时间分为两列。相反,我使用datetime表示in,使用datetime表示out。