[英]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)
當我查詢A時,我需要
Start End
10:12 10:03 (A)
什么是SQL查詢PLS。 謝謝
伙計們,謝謝您的答復。 我的目標是計算時間表。 員工會突擊而出。 我將這些記錄存儲在一張表中。 那張桌子有時間,還有一個地方供打孔或打孔。 員工可能會出於午餐或其他原因外出,並要求外出打孔。我需要扣除這些時間並獲得工作時間。 我的表如下所示:
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之間,所以我需要扣除午餐時間並獲得總時間。 如何在查詢中執行此操作
SELECT max(start), min(end) FROM table WHERE column='A';
如果您要尋找總時間,可以這樣做。
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'
該查詢在Punch ='F'之后找到Punch ='T'的First 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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.