我只是想问一下如何使用不同的timeIn
和timeOut
管理重复行。我的意思是,结果应该是没有重复的行具有相同的id
,但同时该特定人的timeIn
应该是最小timeIn
而timeOut
应该是最后timeOut
。 attDate
必须在同一天。如何进行查询,任何想法?
这里有一些简单的代码:
SELECT
id, name, attend_date, Time_in, Time_out,
count(1) as Count_person
FROM
employee
WHERE
attDate = '12/01/2015' AND Time_out IS NOT NULL
GROUP BY
id, name, attend_date, Time_in, Time_out
HAVING
COUNT(1) > 1
//音符。我只是一个初学者,并试图提高自己。
答案 0 :(得分:0)
您可以使用ROW_NUMBER
获取最新的time_in
和最后time_out
:
WITH Cte AS(
SELECT *,
RN_Time_in = ROW_NUMBER() OVER(PARTITION BY id, attDate ORDER BY Time_in),
RN_Time_Out = ROW_NUMBER() OVER(PARTITION BY id, attDate ORDER BY Time_Out DESC)
FROM employee
)
SELECT *
FROM Cte
WHERE
RN_Time_in = 1
AND RN_Time_Out = 1