select a.DAY1+' '+datename(weekday,a.DAY1)as Date,a.employeeID,a.NameID
,case when min(b.date_attendance) IS NOT null then convert(varchar,min(b.date_attendance),108) else N'無刷卡' end as 上班
,case when max(c.date_attendance) IS NOT null then convert(varchar,max(c.date_attendance),108) else N'無刷卡' end as 下班
from (select distinct convert(varchar,date_attendance,112) as DAY1 ,employeeID,NameID
from table_1
where date_attendance >= CONVERT(smalldatetime,'20140901',112) and date_attendance <CONVERT(smalldatetime,'20140930',112)) as a
left join table_1 as b on convert(varchar,b.date_attendance,112)=a.DAY1
and a.employeeID=b.employeeID and a.NameID=b.NameID
and datepart(hour,b.date_attendance) <12
left join table_1 as c on convert(varchar,c.date_attendance,112)=a.DAY1
and a.employeeID=c.employeeID and a.NameID=c.NameID
and datepart(hour,c.date_attendance) >=12
GROUP BY a.DAY1, a.employeeID, a.NameID
我的做法會在 convert(varchar,min(b.date_attendance),108) 再做一個 CASE wWHEN 來判斷時間是否晚於 09:00 是的話就為遲到.
同理在 convert(varchar,max(c.date_attendance),108) 也做一個判斷是否早於 18:00 是的話就為早退.
小弟有加上case when 但一直煩惱結果出不來@@"
小弟好像試出來了,謝謝!
<pre class="c" name="code">
,case when convert(varchar,min(b.date_attendance),108) > '09:01:00' and convert(varchar,min(b.date_attendance),108) < '12:00:00' and datepart(hour,b.date_attendance) <12
then N'遲到' end as 遲到
補了一個遲到的cast when,卻造成刷卡重複的筆數會顯示出來,沒補之前不會出現重複筆數>_<