SQL Query查找过去的第3个工作日,不包括假日和周末

时间:2016-09-26 22:20:56

标签: sql sql-server database

我正在尝试编写sql查询以查找过去的第3个工作日,不包括假日和周末。以下是我的方法,但它没有给出正确的解决方案。请建议

第一种方法:

shutil

第二种方法:

alter function getdate(@givendate datetime)
returns datetime
AS
BEGIN
declare @calcdate datetime
declare @reqdate datetime
declare @count bit
set @count=1
select @calcdate=@givendate-4;
select @reqdate=@calcdate;
while(@calcdate<@givendate)
begin
while(@count=1)
begin
if exists(select 1 from dbo.holidays with(Nolock) where dbo.holidays.date=@reqdate)
begin
select @reqdate=@reqdate-1
end
else
begin
set @count=0
end
end

select @calcdate=@calcdate+1
end
return @reqdate
END

2 个答案:

答案 0 :(得分:1)

对于过去的第3个工作日,并非100%明确,但也许这可能会有所帮助。显然你必须决定假期是什么。

Select [dbo].[SomeFunctionName]('2016-01-20',3)

返回

2016-01-15

自定义功能是:

CREATE FUNCTION [dbo].[SomeFunctionName](@Date date,@nDay int)
Returns Date
As
Begin
 Declare @RetVal Date

;with cteHolidays as (
    Select * from (Values 
        ('2016-01-01','New Year''s Day'),
        ('2016-01-18','Martin Luther King, Jr,'),
        ('2016-02-15','Washington''s Birthday'),
        ('2016-03-25','Good Friday'),
        ('2016-05-30','Memorial Day'),
        ('2016-07-04','Independence Day'),
        ('2016-09-05','Labor Day'),
        ('2016-11-24','Thanksgiving'),
        ('2016-11-25','Black Friday'),
        ('2016-12-26','Christmas Day')
        ) as Holidays (Date,Name)
), cteDays as (
Select *
      ,RowNr = Row_Number() over (Order By RetVal Desc)
 From  [dbo].[udf-Create-Range-Date](DateAdd(DD,-3*@nDay,@Date),@Date,'DD',1)
 Where RetVal not in (Select Date from cteHolidays)
   and DatePart(DW,RetVal) between 2 and 6
)
Select @RetVal=RetVal
 From cteDays 
 Where RowNr=@nDay

Return @RetVal

End

它需要一个通用辅助函数,我用它来创建动态日期范围

辅助函数

CREATE FUNCTION [dbo].[udf-Create-Range-Date] (@DateFrom datetime,@DateTo datetime,@DatePart varchar(10),@Incr int)

Returns 
@ReturnVal Table (RetVal datetime)

As
Begin
    With DateTable As (
        Select DateFrom = @DateFrom
        Union All
        Select Case @DatePart
               When 'YY' then DateAdd(YY, @Incr, df.dateFrom)
               When 'QQ' then DateAdd(QQ, @Incr, df.dateFrom)
               When 'MM' then DateAdd(MM, @Incr, df.dateFrom)
               When 'WK' then DateAdd(WK, @Incr, df.dateFrom)
               When 'DD' then DateAdd(DD, @Incr, df.dateFrom)
               When 'HH' then DateAdd(HH, @Incr, df.dateFrom)
               When 'MI' then DateAdd(MI, @Incr, df.dateFrom)
               When 'SS' then DateAdd(SS, @Incr, df.dateFrom)
               End
        From DateTable DF
        Where DF.DateFrom < @DateTo
    )

    Insert into @ReturnVal(RetVal) Select DateFrom From DateTable option (maxrecursion 32767)

    Return
End

-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','YY',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','DD',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-31','MI',15) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-02','SS',1) 

修改

验证上面列出的示例

Date        Weekday      BusDayNr
2016-01-20  Wednesday    1        << Selected Date
2016-01-19  Tuesday      2
2016-01-18  Monday       -        << Holiday Martin Luther King, Jr
2016-01-17  Sunday       -
2016-01-16  Saturday     -
2016-01-15  Friday       3        << Date Returned
2016-01-14  Thursday
2016-01-13  Wednesday
2016-01-12  Tuesday
2016-01-11  Monday

答案 1 :(得分:0)

这是另一种方法: 如果你把周末和假期推到一张桌子上这样的东西应该有效:

  declare @holidays_weekends table (
dates date)

insert into @holidays_weekends 
values('2016-09-23'),
('2016-09-24'),
('2016-09-25')


declare 
@date date = getdate()

select dateadd("dd", -3 -(
select count(*) 
from  @holidays_weekends
where dates in( 
dateadd("dd",-1,@date),
dateadd("dd",-2,@date),
dateadd("dd",-3,@date),
dateadd("dd",-4,@date),
dateadd("dd",-5,@date),
dateadd("dd",-6,@date),
dateadd("dd",-7,@date))) ,@date) as '3rd_day'