除特定周末外,不包括周末

时间:2017-03-03 09:59:14

标签: sql-server

我在SQL Server中编写了以下查询

(适用QUERY)

select ISNULL(count(WORKHOUR) ,'-') AS present, 
ISNULL((count(*)-count(WORKHOUR)),'-') AS absent,
ISNULL(cast(SUM(DateDiff(MINUTE,  CAST('0:00' as time), WORKHOUR))/60 as nvarchar(3)) + ':' + cast(SUM(DateDiff(MINUTE,  CAST('0:00' as time), WORKHOUR))%60 as nvarchar(2)),'-') AS TOTALWRKHRS,
ISNULL(cast(AVG(DateDiff(MINUTE,  CAST('0:00' as time), WORKHOUR))/60 as nvarchar(3)) + ':' + cast(AVG(DateDiff(MINUTE,  CAST('0:00' as time), WORKHOUR))%60 as nvarchar(2)),'-') AS AVGWRKHRS
from "+**currMonthVal**+" m,"+**currYearVal**+" e WHERE E.EMPID=M.EMPID AND E.LOCATION=M.LOCATION AND M.EMPID = ?
and DATENAME(WEEKDAY, attdate) not IN ('Saturday', 'Sunday') and CONVERT(VARCHAR(10),ATTDATE,3) not in ("+**resultantHL**+")

其中currMonthVal和currYearVal是从外部传递的月和年值,结果HL是holiday_dates(例如' 24/12 / 2016' 25/12 / 2016')外部。

此查询正常。

我有一个名为 WORKING_DAYS 的表格如下:'

COLUMN_ID     COLUMN_NAME     DATA_TYPE
---------------------------------------
1                ID           NUMBER
2               WDATE         DATE
3             INSTITUTE_ID    NUMBER

表格中的样本数据是:

COLUMN_ID     WDATE     INSTITUTE_ID
---------------------------------------
1            05-12-15     1
2            19-11-16     1

表中的这两个日期实际上在相应月份被宣布为额外工作日,并在周六或周日宣布。

我的要求是: 我在上面的查询中排除了所有星期六和星期日[使用DATENAME(WEEKDAY,attdate)而不是IN(' Saturday',' Sunday')]但我不想在WORKING_DAYS表中排除上述两个日期,但包括它们 我希望在相同的查询中尽可能在WHERE子句中执行此操作。

我该怎么办?

注意: 我已经从WORKING_DAYS表中检索了额外的工作日并存储在字符串变量中,并希望在条件中传递该变量值

1 个答案:

答案 0 :(得分:0)

或者您的日期也在WORKING_DAYS就足够了:

替换

DATENAME(WEEKDAY, attdate) not IN ('Saturday', 'Sunday')

通过

(DATENAME(WEEKDAY, attdate) not IN ('Saturday', 'Sunday') or adddate in (select wdate from WORKING_DAYS))