我有一个关于SQL Server的问题。SQL Server中的天差异
表:holidaylist
Date | weekendStatus | Holidaystatus
2015-12-01 | 0 | 0
2015-12-02 | 0 | 0
2015-12-03 | 0 | 0
2015-12-04 | 1 | 0
2015-12-05 | 1 | 0
2015-12-06 | 0 | 1
2015-12-07 | 0 | 0
2015-12-08 | 0 | 0
2015-12-09 | 0 | 1
2015-12-10 | 0 | 0
2015-12-11 | 0 | 0
2015-12-12 | 1 | 1
2015-12-13 | 1 | 0
表:emp
empid | doj | dos
1 | 2015-12-01 | 2015-12-06
2 |2015-12-01 | 2015-12-13
3 |2015-12-03 |2015-12-13
我想摆脱DOS的司法部天差withoutweekenstatusandholidaysstatus 和includeweekendandholidaystatus
我想这样的输出:
Empid | doj | dos |includeweekendandholidays | witoutincludeweekendandholidayslist
1 | 2015-12-01 |2015-12-06 | 5 | 3
2 | 2015-12-01 |2015-12-13 | 12 | 8
3 | 2015-12-03 |2015-12-13 | 10 | 6
我尝试此查询:
select
a.empid, a.doj, a.dos,
case
when b.weekendstatus = 1 and c.Holidaystatus = 1
then datediff(day, c.date, b.date)
end as includeweekenandholidays
case
when b.weekendstatus != 1 or c.Holidaystatus = 1
then datediff(day, c.date, b.date)
end as witoutincludeweekendandholidayslist
from
emp a
left join
holidaylist b on a.doj = b.date
left join
holidaylist c on a.dos = c.date
上面的查询没有给出预期的结果,请告诉我如何编写查询来实现SQL Server此任务
这是什么意思'withoutweekenstatusandholidaysstatus和includeweekendandholidaystatus'? –
尝试使用[日期] BETWEEN [doj]和[dos]加入您的表格一次。这会在开始日期和结束日期之间每天给你一行。你可以在WHERE子句中过滤掉你不想要的记录。现在你将只有你想要的记录,你可以计算。 –
我们需要天差异b/w dos和doj为每个empid和输出需要包括wekenken和holidayststatusdays意味着多少天与wekenstatusandholidays一起工作,没有weenkenstatusandholidaysstatus意味着需要找到他工作了多少天,没有节假日和周末天和周末天找到holidayslist和weekendlist查看holidayslist table.please告诉我如何编写查询以在sql server中完成此任务 – ravi