I am trying to use my bit column "day_business_day_flag" as a way to count rolling business days as another value "rolling_business_day_sequence". day_business_day_flag is set to find if the day is a holiday, weekday/weekend and returns a 1 if it's non holiday and non weekend and 0 is if the day is a holiday or weekend. I am trying to set rolling_business_day_sequence to either add the previous row value for it as a sort of lag function or another way to calculate how many business days have past to the specific date.
[rolling_day_sequence] = datediff(day,@StartDate,day_date)+1,
I use this to count the normal rolling days sequence. This starts at one and everyday adds another number. I want to do the same with my day_business_day_flag column
day_business_day_flag is a bit value that returns a 1 if day_date is a non-weekend/non-holiday
rolling_business_day_sequence is the column I'm trying to populate.
@startdate is 01/01/2000
@enddate is 12/31/2050
day_date is the date of the row which can be anywhere between 2000-01-01 and 2050-12-31.
Let me know if more info is needed to get this working.
I get "Column name or number of supplied values does not match table definition." Most of the time.
What I have tried:
I tried doing
SELECT rolling_business_day_sequence = COUNT(day_business_day_flag) between @startdate and day_date --WHERE day_date >= @StartDate and day_date <= @EndDate AS rolling_business_day_sequence
I also tried
SELECT day_date, SUM(CASE WHEN day_business_day_flag = 1 then 1 else 0 END) AS rolling_business_day_sequence FROM CALENDAR_DIM group by day_date;
Another Try
SELECT day_date, day_business_day_flag, count(day_business_day_flag) FROM CALENDAR_DIM as rolling_business_day_sequence where day_business_day_flag = 1 group by day_date, day_business_day_flag
INSERT INTO CALENDAR_DIM
--SELECT SUM(CASE(day_business_day_flag) WHEN 1 THEN 1 ELSE 0 END) AS rolling_business_day_sequence FROM CALENDAR_DIM
INSERT INTO CALENDAR_DIM
SELECT COUNT(NULLIF(day_business_day_flag,0)) AS rolling_business_day_sequence FROM CALENDAR_DIM
New Try as of April 3rd, 2019
UPDATE [EDW_MDM].[dbo].[FCFCU_CALENDAR_DIM] SET
business_day_flag_int = (SELECT CAST(day_business_day_flag AS INT) FROM FCFCU_CALENDAR_DIM),
lag_rolling_business_day_sequence = (SELECT LAG(day_business_day_flag, 1) OVER (ORDER BY fcfcu_calendar_dim_id) FROM FCFCU_CALENDAR_DIM)
UPDATE [EDW_MDM].[dbo].[FCFCU_CALENDAR_DIM] SET
rolling_business_day_sequence = business_day_flag_int +lag_rolling_business_day_sequence
I'm getting the error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression." When ran as it's own I see the day_date for 01/01/2000 repeated roughly 200 times before it goes to 01/02/2000. Any suggestions will be greatly appreciated because I feel I am close to what I am looking for.