[英]SQL for Min Max Dates
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)
When I query for A, I need 当我查询A时,我需要
Start End
10:12 10:03 (A)
what is the sql query pls. 什么是SQL查询PLS。 Thanks 谢谢
Guys, Thanks for your replies. 伙计们,谢谢您的答复。 My objective is to calculate a time sheet. 我的目标是计算时间表。 Employee will punch in and punch out. 员工会突击而出。 I store those records in one table. 我将这些记录存储在一张表中。 That table has time and a field for in punch or out punch. 那张桌子有时间,还有一个地方供打孔或打孔。 Employee may go out for lunch or other reasons and punches for out and in. I need deduct those times and get the working hours. 员工可能会出于午餐或其他原因外出,并要求外出打孔。我需要扣除这些时间并获得工作时间。 My table will look like below : 我的表如下所示:
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
For (A)'s time 7.14 - 10.01 is the total hours, but he was not there between 12.03 to 01.05 so I need to deduct the lunch time and get the total hours. 因为(A)的时间是总时间7.14-10.01,但他不在12.03到01.05之间,所以我需要扣除午餐时间并获得总时间。 How to do this in Query 如何在查询中执行此操作
SELECT max(start), min(end) FROM table WHERE column='A';
If you looking for total time this will do it. 如果您要寻找总时间,可以这样做。
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'
This query finds the First PunchTime of a Punch='T' after a Punch='F' (non working times). 该查询在Punch ='F'之后找到Punch ='T'的First PunchTime(非工作时间)。 Then you can simply calculate datediff and similar stuff. 然后,您可以简单地计算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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.