我有两张桌子:
Holiday
和EmpLog
我只需要选择Emplog
中不存在的日期(那些日子是星期几而不等于假日)
例如,我今天没有登录:2012年12月18日。
所以输出将是2012年12月18日
答案 0 :(得分:0)
你的意思是这样的
select column1
from thistable
left join otherTable on
thisTable.column1 = otherTable.column1
Where thisTable.column1 IS NULL
答案 1 :(得分:0)
从这个问题来看,我假设如下:
如果这些假设是正确的,那么此代码将起作用:
--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
希望这有帮助。
Rajan的