如何将与星期一相同数量的工作日分配给周末
How to assign a same number of business day as Monday to weekend days
我有一个日期维度 table,我需要在其中添加 NumberofBusinessDay 列,它会告诉我它是工作日的天数。例如:
Date WeekendFlag BusinessDayFlag **NumberofBusinessDay**
01/01/2015 N N 0
01/02/2015 N Y 1
01/03/2015 Y N 2
01/04/2015 Y N 2
01/05/2015 N Y 2
我已经将工作日分配给工作日,但我正在努力为周末分配一个数字,这将与下一个工作日相同 - 星期一(如果星期一是假期,它将是星期二),如上图table。这是我目前所拥有的:
Date WeekendFlag BusinessDayFlag **NumberofBusinessDay**
01/01/2015 N N 0
01/02/2015 N Y 1
01/03/2015 Y N NULL
01/04/2015 Y N NULL
01/05/2015 N Y 2
如有任何帮助,我们将不胜感激
我为工作日分配 BusinessDays 的查询(仅 2015 年数据):
SELECT Date, WeekendFlag, BusinessDayFlag
,NumberofBusinessDay = CASE WHEN WeekendFlag = 'N' THEN
ROW_NUMBER() OVER (Partition BY YearCode, MonthCode ORDER BY WeekendFlag, Date ASC)
END
INTO #test
FROM DimDate
WHERE Date between '01/01/2015' AND '12/31/2015' --for testing purposes only
ORDER BY Date ASC
仅供参考 - 将周末记为工作日的目的是因为我们不想错过周末发生的任何活动(有可能),我们希望在下一个工作日计算这些活动。
在 SQL Server 2012+ 中,您可以使用累加和来执行此操作:
select dd.*,
sum(case when BusinessDayFlag = 'Y' then 1 else 0 end) over (order by date) as NumberOfBusinessDay
from DateDimension;
您可以将其放入更新中:
with toupdate as (
select dd.*,
sum(case when BusinessDayFlag = 'Y' then 1 else 0 end) over (order by date) as newNumberOfBusinessDay
from DateDimension
)
update toupdate
set NumberOfBusinessDay = newNumberOfBusinessDay;
在 SQL 服务器的早期版本中,您可以使用 cross apply
.
做类似的事情
编辑:
根据您的样本数据,您计算的似乎是工作日,而不是工作日。如果是这样,上面只是使用了错误的变量:
with toupdate as (
select dd.*,
sum(case when WeekEndFlag = 'N' then 1 else 0 end) over (order by date) as newNumberOfBusinessDay
from DateDimension
)
update toupdate
set NumberOfBusinessDay = newNumberOfBusinessDay;
这是基于您的示例结果。请注意,这可能是 off-by-1,因此您应该减去 1。不过,根据描述,我不明白为什么第一行的值为 0 而不是 1。
检查下面sql
---- below code will crete table for businessday calculation
create table dimension (calenderdate date,WeekendFlag char(1) ,BusinessDayFlag char(1),NumberofBusinessDay int)
declare @yearstartdate date
declare @yearenddate date
SELECT @yearstartdate = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
SELECT @yearenddate =DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1)
while datediff (dd,@yearstartdate,DATEADD(DD,1,@yearenddate)) <>0
begin
insert into dimension
select @yearstartdate ,
case when datename(DW,@yearstartdate) = 'SUNDAY' or datename(DW,@yearstartdate) = 'SATURDAY'
then 'Y' else 'N' end,
case when datename(DW,@yearstartdate) = 'SUNDAY' or datename(DW,@yearstartdate) = 'SATURDAY'
then 'N' else 'Y' end,
null
set @yearstartdate = dateadd(dd,1,@yearstartdate)
end
-----verify table
SELECT * FROM dimension
-----calculate NumberofBusinessDay number from below code
;with ctebusinessday
as
(
select calenderdate,
sum(
case when BusinessDayFlag = 'Y' then 1 else 0 end
) over (order by calenderdate) as NOfBusinessDay
from dimension
)
update b
set b.NumberofBusinessDay =c.NOfBusinessDay
from dimension b join ctebusinessday c
on b.calenderdate = c.calenderdate
update dimension
set NumberofBusinessDay = NumberofBusinessDay + 1
where weekendflag = 'Y'
--verify table
select * from dimension
您可以将任何日期更新为工作日或周末,并从 NumberofBusinessDay 计算代码部分计算 NumberofBusinessDay。我只将星期六标记为周末并填充 table
谢谢
我有一个日期维度 table,我需要在其中添加 NumberofBusinessDay 列,它会告诉我它是工作日的天数。例如:
Date WeekendFlag BusinessDayFlag **NumberofBusinessDay**
01/01/2015 N N 0
01/02/2015 N Y 1
01/03/2015 Y N 2
01/04/2015 Y N 2
01/05/2015 N Y 2
我已经将工作日分配给工作日,但我正在努力为周末分配一个数字,这将与下一个工作日相同 - 星期一(如果星期一是假期,它将是星期二),如上图table。这是我目前所拥有的:
Date WeekendFlag BusinessDayFlag **NumberofBusinessDay**
01/01/2015 N N 0
01/02/2015 N Y 1
01/03/2015 Y N NULL
01/04/2015 Y N NULL
01/05/2015 N Y 2
如有任何帮助,我们将不胜感激
我为工作日分配 BusinessDays 的查询(仅 2015 年数据):
SELECT Date, WeekendFlag, BusinessDayFlag
,NumberofBusinessDay = CASE WHEN WeekendFlag = 'N' THEN
ROW_NUMBER() OVER (Partition BY YearCode, MonthCode ORDER BY WeekendFlag, Date ASC)
END
INTO #test
FROM DimDate
WHERE Date between '01/01/2015' AND '12/31/2015' --for testing purposes only
ORDER BY Date ASC
仅供参考 - 将周末记为工作日的目的是因为我们不想错过周末发生的任何活动(有可能),我们希望在下一个工作日计算这些活动。
在 SQL Server 2012+ 中,您可以使用累加和来执行此操作:
select dd.*,
sum(case when BusinessDayFlag = 'Y' then 1 else 0 end) over (order by date) as NumberOfBusinessDay
from DateDimension;
您可以将其放入更新中:
with toupdate as (
select dd.*,
sum(case when BusinessDayFlag = 'Y' then 1 else 0 end) over (order by date) as newNumberOfBusinessDay
from DateDimension
)
update toupdate
set NumberOfBusinessDay = newNumberOfBusinessDay;
在 SQL 服务器的早期版本中,您可以使用 cross apply
.
编辑:
根据您的样本数据,您计算的似乎是工作日,而不是工作日。如果是这样,上面只是使用了错误的变量:
with toupdate as (
select dd.*,
sum(case when WeekEndFlag = 'N' then 1 else 0 end) over (order by date) as newNumberOfBusinessDay
from DateDimension
)
update toupdate
set NumberOfBusinessDay = newNumberOfBusinessDay;
这是基于您的示例结果。请注意,这可能是 off-by-1,因此您应该减去 1。不过,根据描述,我不明白为什么第一行的值为 0 而不是 1。
检查下面sql
---- below code will crete table for businessday calculation
create table dimension (calenderdate date,WeekendFlag char(1) ,BusinessDayFlag char(1),NumberofBusinessDay int)
declare @yearstartdate date
declare @yearenddate date
SELECT @yearstartdate = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
SELECT @yearenddate =DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1)
while datediff (dd,@yearstartdate,DATEADD(DD,1,@yearenddate)) <>0
begin
insert into dimension
select @yearstartdate ,
case when datename(DW,@yearstartdate) = 'SUNDAY' or datename(DW,@yearstartdate) = 'SATURDAY'
then 'Y' else 'N' end,
case when datename(DW,@yearstartdate) = 'SUNDAY' or datename(DW,@yearstartdate) = 'SATURDAY'
then 'N' else 'Y' end,
null
set @yearstartdate = dateadd(dd,1,@yearstartdate)
end
-----verify table
SELECT * FROM dimension
-----calculate NumberofBusinessDay number from below code
;with ctebusinessday
as
(
select calenderdate,
sum(
case when BusinessDayFlag = 'Y' then 1 else 0 end
) over (order by calenderdate) as NOfBusinessDay
from dimension
)
update b
set b.NumberofBusinessDay =c.NOfBusinessDay
from dimension b join ctebusinessday c
on b.calenderdate = c.calenderdate
update dimension
set NumberofBusinessDay = NumberofBusinessDay + 1
where weekendflag = 'Y'
--verify table
select * from dimension
您可以将任何日期更新为工作日或周末,并从 NumberofBusinessDay 计算代码部分计算 NumberofBusinessDay。我只将星期六标记为周末并填充 table
谢谢