Microsoft SQL Server 2005
--
Questions
--
Followers
Top Experts
select
thedate,
weekend_or_holiday,
workdaynum=case
when weekend_or_holiday=1 then 0
else row_number() over (
partition by datediff(m,0,thedate), weekend_or_holiday
order by thedate) end
from tbldates
order by thedate
-- Add column
alter table tbldates add workdaynum int
-- update column
;with tmp as (
select
thedate,
weekend_or_holiday,
workdaynum,
setto=case
when weekend_or_holiday=1 then 0
else row_number() over (
partition by datediff(m,0,thedate), weekend_or_holiday
order by thedate) end
from tbldates
)
update tmp set workdaynum=setto
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Something like I've shown below (though I haven't had a chance to test it).
Alternatively if you're able to get another column in your table that will flag a positive '1' against each row that is a workday, you could just SUM for the workdaynum whether it's a weekday or not.
select
thedate,
weekend_or_holiday,
workdaynum= CASE weekend_or_holiday
WHEN 1 THEN (SELECT COUNT(td.weekend_or_holiday) FROM tbldates tbldb WHERE tbldb.thedate<tblda.thedate AND DATEPART(m,tbldb.thedate)=DATEPART(m,tblda.thedate)) AND weekend_or_holiday=0
ELSE row_number() over (
partition by datediff(m,0,thedate), weekend_or_holiday
order by thedate) end
from tbldates tblda
order by thedate






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
select
thedate,
weekend_or_holiday,
workdaynum= CASE weekend_or_holiday
WHEN 1 THEN
(SELECT
COUNT(td.weekend_or_holiday)
FROM tbldates tbldb
WHERE tbldb.thedate<tblda.thedate
AND DATEPART(m,tbldb.thedate)=DATEPART(m,tblda.thedate) AND weekend_or_holiday=0)
ELSE row_number() over (
partition by datediff(m,0,thedate), weekend_or_holiday
order by thedate) end
from tbldates tblda
order by thedate

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
1) This part was working anyway
;WITH tmp as(select
CALENDAR_DATE,
WORK_DAY_NUMBER,
setto=case
when IS_WEEKEND='X' or IS_HOLIDAY = 'X' then 0
else row_number() over (
partition by datediff(m,0,CALENDAR_DATE
order by CALENDAR_DATE) end
from dbo.TIME_DIM
)
update tmp set WORK_DAY_NUMBER = setto
2) This section uses some of your suggestions above to populate the Working Day number for the Weekends and Holidays
--update Holidays and Weekends with preceding workday number
;with holiday_weekends as(select
a.CALENDAR_DATE,
a.WORK_DAY_NUMBER,
setto=case when WORK_DAY_NUMBER = 0 then (select max(b.WORK_DAY_NUMBER) from dbo.TIME_DIM b
where b.CALENDAR_DATE >=
convert(datetime,convert(v
AND b.CALENDAR_DATE <= a.CALENDAR_DATE
and month(b.CALENDAR_DATE)= month(a.CALENDAR_DATE))
else row_number() over (
partition by datediff(m,0,a.CALENDAR_DA
order by CALENDAR_DATE) end
from dbo.TIME_DIM a)
update holiday_weekends set WORK_DAY_NUMBER = setto
3) This last part updates the Working Day number when it is still 0 because it is the first day of the month and I set it to 1 to match the first working day in that month.
--update Working Day Number when it is set to zero because it is the first day of the month
update a
set a.WORK_DAY_NUMBER = 1
from dbo.TIME_DIM a
where a.WORK_DAY_NUMBER = 0
END
update a
set workdaynum = isnull(b.workdaynum, 1)
from tbldates a
outer apply (
select top(1) workdaynum
from tbldates b
where b.thedate < a.thedate and b.thedate > a.thedate - 31
and MONTH(b.thedate) = MONTH(a.thedate)
and b.workdaynum > 0
order by b.thedate desc) b
where a.workdaynum = 0






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Microsoft SQL Server 2005
--
Questions
--
Followers
Top Experts
Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.