1
我的查詢:獲得的最小值和最大值日期
with
CTE (CardID,Name,Surname,Department,CurrentDate,Time_In,Time_Out) as (
select CardID, Name, Surname, Department, convert(char, pocdate, 106) as CurrentDate,
case when GateID in (1,6) then min(convert(char,pocdate,108)) else '' end as Time_In,
case when GateID in (2,5) then max(convert(char,pocdate,108)) else '' end as Time_Out
from tblEmployee
where Name = 'erdal'
group by CardID,Name,Surname,Department,GateID,pocdate
)
select CardID,Name,Surname,Department,CurrentDate,Time_In,Time_Out
from CTE
group by CardID,Name,Surname,Department,CurrentDate,Time_In,Time_Out
order by CardID asc;
結果:
CardID Name Surname Department CurrentDate Time_In Time_Out
--------- ------- --------- ------------ ------------- ----------- ----------
6672983 ERDAL HUZMELI IT 11-Jan-16 12:41:32
6672983 ERDAL HUZMELI IT 11-Jan-16 17:38:21
6672983 ERDAL HUZMELI IT 11-Jan-16 08:01:53
6672983 ERDAL HUZMELI IT 11-Jan-16 08:03:24
6672983 ERDAL HUZMELI IT 11-Jan-16 13:22:22
6672983 ERDAL HUZMELI IT 11-Jan-16 13:26:47
6672983 ERDAL HUZMELI IT 11-Jan-16 17:36:46
有沒有辦法只有最低Time_In
和最大Time_Out
如下找到?
CardID Name Surname Department CurrentDate Time_In Time_Out
--------- ------- --------- ------------ ------------- ----------- ----------
6672983 ERDAL HUZMELI IT 11-Jan-16 08:01:53 17:38:21
只是不要按Time_In和Time_Out分組。 – pavel