Start End
10:01 10:12 (A)
10:03 10:06 (A)
10:05 10:25 (C)
10:14 10:42 (D)
10:32 10:36 (E)当我查询A时,我需要
Start End
10:12 10:03 (A)什么是sql查询请。谢谢
伙计们谢谢你们的回复。我的目标是计算一份时间表。员工会打卡,然后打出去。我把这些记录存储在一张桌子上。那张桌子有时间和一个字段,可以用来打孔或打孔。员工可能因午餐或其他原因而外出,并因外出或进场而挨打。我需要扣除这些时间,然后得到工作时间。我的桌子将如下所示:
PunchTime EmpCode IsInpunch
10:01 AM (A) T
12:03 PM (A) F (this isoutpunch)
01:05 PM (A) T
07:14 PM (A) F
10:32 AM (B) T因为(A)的时间是7.14-10.01小时,但他没有在12.03到01.05之间,所以我需要扣除午餐时间并得到总时间。
发布于 2011-02-14 17:17:39
SELECT max(start), min(end) FROM table WHERE column='A';发布于 2011-02-14 17:26:34
如果你想要总时间,这就行了。
DECLARE @testData table (
Punchtime datetime, empcode char(3), isInPunch char(1))
INSERT INTO @testData
Values
('10:01 AM ', '(A)', 'T'),
('12:03 PM', '(A)', 'F'),
('01:05 PM', '(A)', 'T'),
('07:14 PM', '(A)', 'F'),
('10:32 AM', '(B)', 'T')
;WITH CTE as(
SELECT
DENSE_RANK() over (Partition by empcode , isInPunch Order by punchTime) id,
Punchtime,
empcode,
isInPunch
FROM
@testData
WHERE
empcode = '(A)'
)
SELECT
Cast(cast(sum(
cast(outTime.punchTime as float) - cast(inTime.punchTime as float)
)as datetime) as time)
FROM
CTE inTime
INNER JOIN CTE outTime
ON inTime.empcode = outTime.empcode
AND inTime.id = outTime.id
AND inTime.isInPunch = 'T'
and outTime.isInPunch = 'F'发布于 2011-02-14 19:48:03
此查询在Punch='F‘(非工作时间)之后查找Punch='T’的第一个PunchTime。然后,您可以简单地计算datediff和类似的内容。
SELECT EmpCode,
PunchTime StartTime,
(SELECT TOP 1 PunchTime from Table1
where IsInpunch = 'T' and EmpCode=T.EmpCode and PunchTime>T.PunchTime
order by PunchTime) EndTime
FROM Table1 T
WHERE IsInpunch = 'F'https://stackoverflow.com/questions/4995031
复制相似问题