预期产出:
SL# Emp# Employee Name IN OUT
1 106 George Jacob 24/Sep/2017 8:08:00AM 24/Sep/2017 12:53:00PM 04:45:00
24/Sep/2017 2:13:00PM 24/Sep/2017 5:58:00PM 03:45:00
25/Sep/2017 8:12:00AM 25/Sep/2017 6:02:00PM 09:50:00
26/Sep/2017 8:18:00AM 26/Sep/2017 2:15:00PM 05:57:00
26/Sep/2017 2:32:00PM 26/Sep/2017 6:00:00PM 03:28:00
27/Sep/2017 8:02:00AM 27/Sep/2017 5:57:00PM 09:55:00
28/Sep/2017 8:01:00AM 28/Sep/2017 6:01:00PM 10:00:00
01/Oct/2017 8:16:00AM 01/Oct/2017 5:56:00PM 09:40:00
02/Oct/2017 7:58:00AM 02/Oct/2017 5:56:00PM
我尝试了这个查询,但没有得到如上所述的确切输出:
SELECT Row_number()
OVER (ORDER BY A.dt ASC) AS SNo,
CONVERT(DATE, A.dt)
--CONVERT(VARCHAR(26), A.DT, 103) as DATEEVENT,
b.emp_code,
B.emp_name,
F.event_entry_name,
a.dt,
Cast(LEFT(CONVERT(TIME, a.dt), 5) AS VARCHAR) AS 'time',
Isnull(B.areaname, 'OAE6080036073000006') AS areaname,
C.dept_name,
b.emp_reader_id,
Isnull(c.dept_name, '') AS group_name,
CONVERT(CHAR(11), '2017/12/30', 103) AS StartDate,
CONVERT(CHAR(11), '2018/01/11', 103) AS ToDate,
0 AS emp_card_no
FROM dbo.trnevents AS A
LEFT OUTER JOIN dbo.employee AS B
ON A.emp_reader_id = B.emp_reader_id
LEFT OUTER JOIN dbo.departments AS C
ON B.dept_id = C.dept_id
LEFT OUTER JOIN dbo.devicepersonnelarea AS E
ON A.pointid = E.areaid
LEFT OUTER JOIN dbo.event_entry AS F
ON A.eventid = F.event_entry_id
答案 0 :(得分:0)
您可以使用子选择或CTE来获取员工订购的数据,并将其用作主数据表。类似的东西(必要时对其进行调整):
;with ordered as (
select
emp_reader_id as empId,
CONVERT(DATE, dt) as Punch,
Row_number()
OVER (PARTITION BY emp_reader_id ORDER BY CONVERT(DATE, dt) ASC) as OrderedPunch
from trnevents
)
SELECT
entered.empId,
entered.Punch as PunchIn,
exited.Punch as PunchOut
from
ordered as entered
left join ordered as exited on
entered.empId = exited.empId
and entered.OrderedPunch + 1 = exited.OrderedPunch
说明:'ordered'CTE确实显示按日期排序的员工入口/出口。由于ROW_NUMBER
,为每个员工重置PARTITION BY
(我假设emp_reader_id确实包含员工ID)。
一旦我获得了每个员工的计数器,我就为每个员工加入每个员工(左连接中的第一个条件),并为该员工提供下一个打卡(左连接中的第二个条件)。这样我就可以显示输入栏和出口(下一拳)。
在数据中输入和输出列后,您可能希望排除一些数据(每个员工的奇数行是您想要的行)添加WHERE entered.OrderedPunch %2 = 1