-2

我有两张桌子:

HolidayEmpLog

我只需要选择不存在的日期Emplog(那些日子是 WEEKDAYS 并且不等于仅限假期)

例如,我没有记录这一天:2012 年 12 月 18 日。

所以输出将是 2012 年 12 月 18 日

4

2 回答 2

0

根据这个问题,我假设以下内容:

  1. 您有一个 EmpLog 表,其中包含员工在场的所有日期的日期戳。
  2. 您有一个假期表,其中包含周末或假期的日期列表
  3. 您需要找出所有工作日的日期,但未记录给定员工

如果这些假设是正确的,那么此代码将起作用:

--Declare variables to hold date values 
DECLARE @MyDate    DATETIME, 
        @FirstDate DATETIME, 
        @LastDate  DATETIME 

SELECT @MyDate = Getdate() -- OR Pass whatever date you want 

--Populate variable with values of first date and last date of the given month 
SELECT @FirstDate = CONVERT(VARCHAR(25), Dateadd(dd, -( Day(@mydate) - 1 ), 
                                         @mydate), 
                           101), 
       @LastDate = CONVERT(VARCHAR(25), Dateadd(dd, -( Day( 
                                        Dateadd(mm, 1, @mydate)) ), 
                                                    Dateadd(mm, 1, @mydate)), 
                   101) 

--Declare table variable to hold date values for the entire month 
DECLARE @AllDates TABLE 
  ( 
     datevalue DATETIME 
  ) 
DECLARE @Lastday INT 

SET @Lastday = Datepart(d, @LastDate) 

DECLARE @Days INT 

SET @Days = 1 

-- Populate table variable with all days of the month 
WHILE @Days <= @Lastday 
  BEGIN 
      INSERT INTO @AllDates 
      SELECT @FirstDate 

      SET @FirstDate = Dateadd(d, 1, @FirstDate) 
      SET @Days = @Days + 1 
  END 

--Query joining the EmpLog table and Holiday Table, using Left Outer Join  
SELECT AD.datevalue 
FROM   @AllDates AD 
       LEFT OUTER JOIN emplog EL 
                    ON AD.datevalue = EL.datevalue 
       LEFT OUTER JOIN holiday H 
                    ON AD.datevalue = H.datevalue 
-- Where clause to return only dates that do not appear in EmpLog or Holiday tables 
WHERE  EL.datevalue IS NULL 
       AND H.datevalue IS NULL  

希望这可以帮助。

拉詹

于 2012-12-18T05:22:32.793 回答
0

你的意思是这样的

select column1 
from thistable 
left join otherTable on 
thisTable.column1 = otherTable.column1 
Where thisTable.column1 IS NULL
于 2012-12-18T03:15:53.663 回答