我有一個關於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