SQL BigQuery: How to populate dates from rows cycle_base and cycle_interval - sql

I'm having trouble populating dates with variable cycle_base (day of week) and cycle_interval (days) columns in Google BigQuery SQL.
The idea is to populate a date array for 2022 for each product where the dates fall within the valid_from and valid_to dates and where the dates are generated with the respective cycle_interval
A snippet from my data looks like this:
cycle_base
valid_from
valid_to
cycle_interval
product
2016-09-19
2020-04-20
2022-12-31
7
A
2018-12-17
2020-01-27
2022-12-31
28
B
2019-12-30
2020-01-27
2022-12-31
56
C
I tried generating a date array and then joining those dates on the DAYOFWEEK, which ofcourse only works for rows with a interval of 7 days. But I can't seem to find a way to achieve the above with the other intervals.
Edit
Expected data for 2022:
The cycle_base represents the day of the week. The exact date is irrelevant really. Edit: The date represents the starting point from which to interval the weeks.
Product A starts on monday for every week.
Product B starts on monday for every 3 weeks.
Product C starts on monday for every 8 weeks.
date
product
dayofweek
cycle_interval
2022-01-03
A
monday
7
2022-01-10
A
monday
7
...
...
...
...
2022-01-03
B
monday
28
2022-01-17
B
monday
28
...
...
...
...
2022-01-03
C
monday
56
2022-02-21
C
monday
56
...
...
...
...
Hope someone can point me in the right direction :)
Thanks in advance,
Glenn

Related

Ability to count per hour per day?

I'm new to SSAS so be gentle!
I have (simplified):
fact table that has an ID, start date, start datetime, end date, end datetime
A date dimension that has a granularity from Year to Calendar Date.
What I'd like to be able to do is get the count of ID per hour per date member/current member. However I'm not exactly sure how to get there.
Fact Table Example
ID
Start Date
End Date
Start DateTime
End DateTime
1
2022-01-01
2022-01-04
2022-01-01 23:00
2022-01-04 05:33
53
2022-01-01
2022-01-07
2022-01-01 04:00
2022-01-07 12:05
Wanted results:
Date
Hour
Count
2022-01-02
00:00
1
2022-01-02
01:00
1
2022-01-02
02:00
1
2022-01-02
03:00
1
2022-01-02
04:00
2
2022-01-02
05:00
2
I expect I need an hour dimension that somehow links to the date dimension and then some sort of measure that does a between comparison but not exactly sure how to go about this.
Any help is appreciated!
Edit: above tables may not be showing right for some reason. Looks great when I go to edit them...

Create a counter based on days of the week, Fri-Mon treated as a bucket

Today/Getdate is always going to be the starting date. A counter starts at 0; let's call it TheResult. If the day is Friday/Saturday/Sunday/Monday, then they all receive the same number in the counting process. The accrual picks back up on Tuesday.
TheDate
TheDayName
TheResult
2022-06-07
Tuesday
0
2022-06-08
Wednesday
1
2022-06-09
Thursday
2
2022-06-10
Friday
3
2022-06-11
Saturday
3
2022-06-12
Sunday
3
2022-06-13
Monday
3
2022-06-14
Tuesday
4
2022-06-15
Wednesday
5
2022-06-16
Thursday
6
2022-06-17
Friday
7
2022-06-18
Saturday
7
2022-06-19
Sunday
7
2022-06-20
Monday
7
2022-06-21
Tuesday
8
What would the sql code need to be to achieve the values in the TheResult column?
I'm not sure how to approach the problem because of the Fri-Mon bucket of days, so I have not made any progress. I've tried finding similar questions asked using Google, but it seems I'm searching for the wrong keywords to find the answer to my question.

How to match the closet date in sql (redshift)?

For example, my table A is work_schedule:
Employee_id
Week_start
Work_schedule
A
2021-01-03
Day shift
A
2021-01-10
Day shift
A
2021-01-17
Night shift
B
2020-12-27
Day shift
B
2021-01-03
Day shift
Table B is employee_history:
Employee_id
Calendar_date
Tenure
A
2020-12-20
0
A
2020-12-21
1
A
---
2-30
A
2021-01-19
31
A
2021-01-20
32
B
2020-12-15
0
B
2020-12-16
1
B
---
Employee can choose work schedule 2 weeks ahead, and I want to fetch tenure at the snapshot date (2 weeks ahead). For employee A, the 14 days time period can match a calendar_date. But for employee B, he started within 2 weeks. I want to have the closet date to the 2-week date.
The ideal output is:
Employee_id
Week_start
Work_schedule
Calendar_date (to calculate tenure)
Tenure (at 2 weeks ago)
A
2021-01-03
Day shift
2020-12-20
0
A
2021-01-10
Day shift
2020-12-27
7
A
2021-01-17
Night shift
2021-01-03
14
B
2020-12-27
Day shift
2020-12-15
0
B
2021-01-03
Day shift
2020-12-20
5
For one record to fetch closet date, I can use
order by abs(datediff(day, (week_start - 14), calendar_date)) asc
limit 1
For example, fetch ‘2020-12-15’ as the closest date to ‘2020-12-13’.
select employee_id, calendar_date, tenure
from employee_history h
where employee_id = B
order by abs(datediff(day, ('2020-12-27' - 14), date_key)) asc
limit 1
But I have more than one employees in this situation, how can I get the closest calendar_date for all those that cannot find a match for exactly 2 weeks?

Is it possible to convert integer to days and hours in SQL?

I am using SQL Server 2014.
What I'm trying to do is add a new time to an old datetime.
I'm not even sure if it's possible but I thought I'd ask the experts.
So these are what my columns look like:
CurrentDate | Hours | NewDate
2017-03-10 08:00:00 | 25 | ??
2017-01-01 10:00:00 | 27 | ??
What I want is the Hours to be converted to days and hours so it can be added to the CurrentDate to create a NewDate.
So the NewDate would be: 2017-03-11 09:00:00 because 25 hours equates to 1 day and 1 hour. And the second NewDate would be: 2017-01-02 01:00:00 because 27 equates to 1 day and 3 hours.
I actually don't think this is possible and there's a chance I might have to put the hours already converted into days and times but if that's the case, how would I write 25 hours? Would it be 00-00-01 01:00:00? And would 27 hours be 00-00-01 03:00:00 and then just add those values into CurrentDate?
Thanks! Feel free to tell me this has been asked before (I looked, but couldn't find anything as unique as this or maybe I didn't look hard enough) or if this can't be done.
You can simply use DATEADD, no need to convert the hours to days first:
SELECT CurrentDate,
Hours,
DATEADD(HOUR,Hours,CurrentDate) NewDate
FROM dbo.YourTable;
You can try this:
select DATEADD(HOUR,25,'2017-03-10 08:00:00') -- 2017-03-11 09:00:00.000
select DATEADD(HOUR,27,'2017-01-01 10:00:00') -- 2017-01-02 13:00:00.000

SQL How Many Employees Are Working, Group By Hour

I have a table, a timetable, with check-in and check-out times of the employees:
ID Date Check-in Check out
1 1-1-2011 11:00 18:00
2 1-1-2011 11:00 19:00
3 1-1-2011 16:00 18:30
4 1-1-2011 17:00 20:00
Now I want to know how many employees are working, every (half) hour.
The result I want to see:
Hour Count
11 2
12 2
13 2
14 2
15 2
16 3
17 3
18 2,5
19 1
Every 'Hour' you must read as 'till the next full hour', ex. 11 -> 11:00 - 12:00
Any ideas?
Build an additional table, called Hours, containing the following data:
h
00:00
00:30
01:00
...
23:30
then, run
Select h as 'hour' ,count(ID) as 'count' from timetable,hours where [Check_in]<=h and h<=[Check_out] group by h