利用SQL统计时间时怎么去除"周末和假日"的时间(非工作日时间)
表一 (基本信息表)
file_uid empno deptcode timeofbegin timeofend days
.... .... .... .... .... ....
.... .... .... .... .... ....
.... .... .... .... .... ....
1001 c06001 cx100 2006-09-28 2006-10-18 20
1002 c06002 cx101 2006-09-30 2006-10-09 9
1002 c06003 cx102 2006-10-08 2006-10-18 10
1002 c06004 cx103 2006-09-29 2006-10-20 21
1003 c06005 cx104 2006-10-10 2006-10-16 6
1004 c06006 cx105 2006-10-09 2006-10-09 0
.... .... .... .... .... ....
.... .... .... .... .... ....
.... .... .... .... .... ....
表二 Calendar.dbo
DateOfThis EventType_FK WorkingDay_Flag
... ... ...
... ... ...
... ... ...
... ... ...
2006-09-28 85 1
2006-09-29 85 1
2006-09-30 82 1
2006-10-01 82 0
2006-10-02 82 0
2006-10-03 82 0
2006-10-04 82 0
2006-10-05 82 0
2006-10-06 82 0
2006-10-07 82 0
2006-10-08 85 1
2006-10-09 85 1
2006-10-10 85 1
2006-10-11 85 1
2006-10-12 85 1
2006-10-13 85 1
2006-10-14 82 0
2006-10-15 82 0
2006-10-16 85 1
2006-10-17 85 1
2006-10-18 85 1
2006-10-19 85 1
2006-10-20 85 1
2006-10-21 82 0
2006-10-22 82 0
... ... ...
... ... ...
... ... ...
... ... ...
其中 2006-10-01 到 2006-10-07 为放假日期,
WorkingDay_Flag 为0 表示 周末和假期
问题:
现在要求把周末和假日的时间去掉
结果应该如下:
-------------------------------------
.... .... .... .... .... ....
1001 c06001 cx100 2006-09-28 2006-10-18 11
1002 c06002 cx101 2006-09-30 2006-10-09 2
1002 c06003 cx102 2006-10-09 2006-10-09 0
.... .... .... .... .... ....
-------------------------------------
[解决办法]
Select
A.file_uid,
A.empno,
A.deptcode,
A.timeofbegin,
A.timeofend,
Count(*) - 1 As days
From
表一 A
Inner Join
表二 B
On DateOfThis Between A.timeofbegin And A.timeofend And WorkingDay_Flag = 1
Group By A.file_uid, A.empno, A.deptcode, A.timeofbegin, A.timeofend