如何在没有日志表的日子里缺席? [英] How to get absent days where the days not in log table ?
本文介绍了如何在没有日志表的日子里缺席?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在#dates表中缺席,如果日期不在日志表中
任何帮助将不胜感激
< b>我尝试过:
Declatr @ DateT 表(D 日期);
选择 @ d1 = ' 10/1/2013',@ d2 = ' 2013年10月31日跨度>;
; 日期( date )
as
(
选择 @ d1
union 所有
选择 dateadd(d, 1 , date )
来自日期
其中 日期< @ d2
)
插入 进入 @ DateT
选择 日期 as CountedDays 来自日期;
选择 不同 ID,转换( time ,Log_D),Late,EarlyOut,AbsentOne,Day(Log_D) as day1 From ( SELECT m.ID,L.Log_D,LC,T.EnterAllow_FD,W.FirstStartTime, T.Late_FD,T.EarlyOut_FD,T.OutAllow_FD,W.FirstEndTime,a.StartDate,a.EndDate,W.id WID,
( CASE WHEN convert ( time , L.Log_D)> Dateadd(mi,T.Late_FD,W.FirstStartTime)和 convert (< span class =code-keyword> time ,L.Log_D)< Dateadd(mi,T.EnterAllow_FD + T.Late_FD,W.FirstStartTime)和 COUNT(*) OVER ( PARTITION BY L.EmpId,day(L.Log_D))= 2 和 LC = 1
然后 ' Ê' end ) as 晚了,
( CASE WHEN convert ( time ,L.Log_D)< Dateadd(mi,-T.EarlyOut_FD,W.FirstEndTime)和 convert ( time ,L.Log_D)> Dateadd(mi, - (T。 EarlyOut_FD + T.OutAllow_FD),W.FirstEndTime)和 COUNT(*) OVER ( PARTITION BY L.EmpId,day(L.Log_D))= 2 和 LC = 2
然后 ' خ,م' end ) as EarlyOut,
( CASE WHEN (COUNT(*)< span class =code-keyword> OVER ( PARTITION BY L.EmpId,day (L.Log_D))< 2 )
或( convert ( time ,L.Log_D)> Dateadd(mi,T.EnterAllow_FD + T.Late_FD,W.FirstStartTime)
和 convert ( time ,L.Log_D)< Dateadd(mi , - (T.EarlyOut_FD + T.OutAllow_FD),W.FirstEndTime))那么 ' غ' END ) as [AbsentOne]
来自 WorkPeriod W,LogT L 完整 加入 MinimumInfoT m m.ID = L.EmpId,TimeRulesT T,WorkPeriod a2 join WorkPeriod a on a2.id = a.PeridNo 其中月(L.Log_D)= 10
和年(L.Log_D)= 2013
和 LC ( 1 ,< span class =code-digit> 2 )
和 L.Log_D 不 a.StartDate 和 a.EndDate
和 M.ID = L.EmpId
和 W.id = m.W_Period
和 W.id = T.WorkPeriodID
和 W.id = a.PeridNo) as tt, @ DateT T2 order by ID,day1
解决方案
最简单的方法是使用JOIN来连接f过滤数据。
我不打算通过你的代码试图找出你的表模式,所以我创建了一组虚拟表来证明这一点:创建 表员工(EmpId int 身份( 1 , 1 ),Name nvarchar ( 125 ))
将 插入员工值(' Joe Bloggs')
创建 表 attendanceLog
(
id int identity ( 1 , 1 ),
EmpId int ,
DateLogged 日期
)
插入 进入 attendanceLog 值
( 1 ,' 2013-10- 02'),
( 1 ,' 2013-10-03'),
( 1 ,' 2013-10-04'),
( 1 ,' 2013-10-05'),
( 1 ,' 2013-10-06'),
( 1 ,' 2013-10-07'),
(1,' 2013-10-08' )
以下查询将列出(您的)@DateT表中未出现在(my)AttendanceLog表中的日期:选择 D
来自 @ DateT D
left 外部 JOIN attendanceLog AL ON AL.DateLogged = DD
WHERE EmpId IS NULL注意我使用了
日期
列类型。您似乎在代码中使用了大量CONVERT
函数,因此您可能需要将ON
子句更改为< pre lang =SQL> ON CAST(AL.DateLogged AS 日期)= DD
使用此代码
SELECT DATENAME(dw,GETDATE()) union all
SELECT DATENAME(dw,DATEADD(dd, 1 ,GETDATE())) union 全部
SELECT DATENAME(dw,DATEADD(dd, 2 ,GETDATE())) union all
SELECT DATENAME(dw,DATEADD(dd,<小号pan class =code-digit> 3 ,GETDATE())) union 全部
SELECT DATENAME(dw,DATEADD(dd, 4 ,GETDATE())) union all
SELECT DATENAME(dw) ,DATEADD(dd, 5 ,GETDATE())) union 全部
SELECT DATENAME(dw,DATEADD(dd, 6 ,GETDATE ()))
I need to get absent days from #dates table if the days not in log table
any help will be appreciated
What I have tried:
Declatr @DateT Table(D Date);
select @d1 = '10/1/2013',@d2= '10/31/2013';
;with dates ( date )
as
(
select @d1
union all
select dateadd(d,1,date)
from dates
where date < @d2
)
insert into @DateT
select date as CountedDays from dates ;
Select distinct ID,convert(time,Log_D),Late,EarlyOut,AbsentOne,Day(Log_D) as day1 From(SELECT m.ID,L.Log_D,L.C,T.EnterAllow_FD,W.FirstStartTime,T.Late_FD,T.EarlyOut_FD,T.OutAllow_FD,W.FirstEndTime,a.StartDate,a.EndDate,W.id WID,
(CASE WHEN convert(time,L.Log_D)>Dateadd(mi,T.Late_FD,W.FirstStartTime) and convert(time,L.Log_D)<Dateadd(mi,T.EnterAllow_FD+T.Late_FD,W.FirstStartTime) and COUNT(*) OVER (PARTITION BY L.EmpId,day(L.Log_D)) = 2 and L.C =1
then 'ت'end )as Late,
(CASE WHEN convert(time,L.Log_D)<Dateadd(mi,-T.EarlyOut_FD,W.FirstEndTime) and convert(time,L.Log_D)>Dateadd(mi,-(T.EarlyOut_FD+T.OutAllow_FD),W.FirstEndTime)and COUNT(*) OVER (PARTITION BY L.EmpId,day(L.Log_D)) = 2 and L.C =2
then 'خ,م'end )as EarlyOut,
(CASE WHEN (COUNT(*) OVER (PARTITION BY L.EmpId,day(L.Log_D)) < 2)
or (convert(time,L.Log_D)>Dateadd(mi,T.EnterAllow_FD+T.Late_FD,W.FirstStartTime )
and convert(time,L.Log_D)<Dateadd(mi,-(T.EarlyOut_FD+T.OutAllow_FD),W.FirstEndTime)) THEN 'غ' END) as [AbsentOne]
from WorkPeriod W,LogT L full join MinimumInfoT m on m.ID =L.EmpId ,TimeRulesT T,WorkPeriod a2 join WorkPeriod a on a2.id=a.PeridNo where month(L.Log_D) =10
and year(L.Log_D) =2013
and L.C in(1,2)
and L.Log_D not Between a.StartDate and a.EndDate
and M.ID =L.EmpId
and W.id =m.W_Period
and W.id=T.WorkPeriodID
and W.id=a.PeridNo) as tt,@DateT T2 order by ID,day1
解决方案
The easiest way to do this is to use a JOIN to filter the data.
I wasn't going to wade through your code to try and work out your table schemas so I created a dummy set of tables to demonstrate the point :create table Employee (EmpId int identity(1,1), Name nvarchar(125)) insert into Employee values ('Joe Bloggs') Create Table attendanceLog ( id int identity(1,1), EmpId int, DateLogged Date ) insert into attendanceLog values (1, '2013-10-02'), (1, '2013-10-03'), (1, '2013-10-04'), (1, '2013-10-05'), (1, '2013-10-06'), (1, '2013-10-07'), (1, '2013-10-08')
The following query will then list the dates from (your) @DateT table that do not appear in (my) AttendanceLog table:select D from @DateT D left outer JOIN attendanceLog AL ON AL.DateLogged=D.D WHERE EmpId IS NULLNote I have used a
Date
column type. You appear to be using a lot ofCONVERT
functions in your code so you may need to change theON
clause toON CAST(AL.DateLogged AS Date)=D.D
use this code
SELECT DATENAME(dw, GETDATE()) union all SELECT DATENAME(dw, DATEADD(dd, 1, GETDATE())) union all SELECT DATENAME(dw, DATEADD(dd, 2, GETDATE())) union all SELECT DATENAME(dw, DATEADD(dd, 3, GETDATE())) union all SELECT DATENAME(dw, DATEADD(dd, 4, GETDATE())) union all SELECT DATENAME(dw, DATEADD(dd, 5, GETDATE())) union all SELECT DATENAME(dw, DATEADD(dd, 6, GETDATE()))
这篇关于如何在没有日志表的日子里缺席?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文