select orderdate, shipdate, ordernum
from dbo.orders
另一张表有
select date, workingday
from a.workdays
表工作日将workingday
字段定义为Y,N,将周末和假日定义为N.
我想计算orderdate
和shipdate
之间的差异来挑选任何ordernum
,天数差异大于5.如何将它们连接在一起以便参考。我在网上研究了很多pp使用变量,遗憾的是我对此并不熟悉。
我成功地排除了周末,但不知道如何处理假期。
DATEDIFF(dd, shipdate, orderdate + 1) - DATEDIFF(wk, orderdate, shipdate) * 2 - (CASE WHEN datename(dw, orderdate) = 'sunday' THEN 1 ELSE 0 END) - (CASE WHEN datename(dw, shipdate) = 'saturday' THEN 1 ELSE 0 END) AS DaysDifference
由于
答案 0 :(得分:1)
我倾向于使用连接和聚合函数来解决这个问题:
select o.ordernum
from dbo.orders o
join a.workdays w
on a.workdays >= o.orderdate and a.workdays <= o.shipdate
where workingday = 'Y'
group by o.ordernum
having count(*) > 5