Insert into @temp3
select
Distinct t.staffName,
attendDate = convert(date,t.AttendDate)
, t.staffId
, t.Firmid
,t.ShiftName
, t.attendId
, CheckIn = convert(time(0),t.AttendDate)
, CheckOut = convert(time(0),x.AttendDate)
from @temp2 as t
outer apply (
select top 1 i.AttendDate
from @temp2 as i
where t.staffid = i.staffid
and i.attendDate > t.attendDate
order by i.attendDate desc
) x
where t.InOutMode = 'CheckIn'
[ 请检查第一张图片我想先获得CheckIn和Last Check Out
我有以下结果我想将checkIn和checkOut显示为列 喜欢
-------------------------------------------------------------------------------
atteDate | staffName | staffId | Firmid |attendId | CheckIn |CheckOut |
--------------------------------------------------------------------------------
2014-11-13|Urvashi A Patel |7023 |6012 | 204 | 9:30:03 | 18:34:17
答案 0 :(得分:3)
使用聚合查询,按attendDate
的日期进行分组。
select
attendDate = convert(date,t.AttendDate)
, t.staffName
, t.staffId
, t.Firmid
, attendId = min(t.attendId)
, CheckIn = min(convert(time(0),t.AttendDate))
, CheckOut = max(convert(time(0),t.AttendDate))
from t
group by
t.staffName
, t.staffId
, t.Firmid
, convert(date,t.attendDate)
如果您收到错误,因为您尝试使用某些查询来提供上述结果,那么最好在您的问题中包含该查询。
如果是这种情况,那么使用common table expression可能有所帮助。
;with cte as (
/* query that does stuff to get the results in your question */
)
select
attendDate = convert(date,t.AttendDate)
, t.staffName
, t.staffId
, t.Firmid
, attendId = min(t.attendId)
, CheckIn = min(convert(time(0),t.AttendDate))
, CheckOut = max(convert(time(0),t.AttendDate))
from t
group by
t.staffName
, t.staffId
, t.Firmid
, convert(date,t.attendDate)