I have 2 fields datetime (TaskStartDate and TaskEndDate). I created another field and called it Duration.
I need to update Duration to reflect how many business hours and minutes between the 2 fields (TaskStartDate and TaskEndDate) noting that we work only 8 hours a day.
Example:
TaskStartDate 2014-01-01 07:30
TaskEndDate 2014-01-03 15:30 Duration should read 24 Hours not 72hours
Related
I"m dealing with a dataset with 4 week sales data (data will be refreshed every hour) and need to observer for abnormality
I think I'll go with a basic approach, to compare with average numbers and I'm trying to figure out how to best break this down so I can answer some questions below
On average, how many orders received at 9:00 , 15:00 or 16:00 past 30 days
On average, how many orders received at 9:00 every Wednesday (past 4 Wednesdays), at 15:00 every Thursday (past 4 Thursdays),
Not sure how do we go about this (after breaking date/time down to Hour and Weekday columns)
date
order ID
order hour
order weekday
10/07/2022 10:26:12 PM
1111
22
6
10/07/2022 10:27:12 PM
2222
22
6
....
....
....
....
19/07/2022 11:34:19 AM
9998
11
1
19/07/2022 11:34:35 AM
9999
11
1
I would love to get your advice please
Thanks
I've ended up going with a tedious approach.
#get current hour & weekday
now=datetime.datetime.now()
today=datetime.date.today()
current_hour=now.hour
current_weekday=today.weekday()
#create a DF with orders from the same hour & weekday window
same_hour_weekday_df=order_df[(order_df.order_hour==current_hour ) & (order_df.order_weekday==current_weekday) ]
#calculate avg orders generated from the past weeks within the same hour and weekyday timeframe
orders_same_hour_weekday=same_hour_weekday_df['order_created_at'].count()
same_hour_weekday_periods=same_hour_weekday_df['order_week'].nunique()
avg_orders_same_hour_weekday=orders_same_hour_weekday/same_hour_weekday_periods
We currently have a master table stored in our SQL server with the following example information:
Site
Shift Num
Start Time
End Time
Daily Target
A
1
8:00AM
4:00PM
10000
B
1
7:00AM
3:00PM
12000
B
2
4:00PM
2:00AM
7000
C
1
6:00AM
2:00PM
5000
As you can see, there are multiples sites each with their own respective shift start & end times as well as a total daily target for the day.
Another table in the DB is populated by users via the use of a PowerApp. This PowerApp will push output values to the server like so:
Site
Shift Number
Output
Timestamp
A
1
2500
3/15/2022 9:45 AM
A
1
4200
3/15/2022 11:15 AM
A
1
5600
3/15/2022 12:37 PM
A
1
7500
3/15/2022 2:15 PM
This table contains a log of all time-stamped output entries for each site / shift.
What I would like to do is do a daily trend of output vs. target. In order to do so, all output values over a specific shift would have to be aggregated in a SUM function for a given shift grouped by the shift day. The resulting view would need to look like this:
Site
Shift Number
Day
Actual
Target
A
1
3/14
9500
10000
B
1
3/14
13000
12000
A
1
3/15
8000
10000
B
1
3/15
10000
12000
This is easy enough for daytime shifts (group by day and sum the output values). However, if you notice in the master table, Site B / Shift 2 crosses midnight. In this example, I would need to sum values from the previous day 4PM up until 2AM of today. The date grouping would be done by the Shift End Time. Here's an example of the problem area:
Site
Shift Number
Output
Timestamp
B
2
3300
3/15/2022 5:45 PM
B
2
2200
3/15/2022 8:15 PM
B
2
1600
3/16/2022 12:37 AM
B
2
2500
3/16/2022 1:15 AM
I would need these four rows to be aggregated in the view as one row like so:
Site
Shift Number
Day
Actual
Target
B
2
3/16
9600
10000
The values should be listed under March 16th since the end time of the shift occurs then. The values are summated and the target is taken from the daily target master table.
How can I properly calculate these outputs for each shift every day irrespective if it crosses into a new day or not in a view? Or should I go a different route altogether?
I'm fairly new to this forum and to T-SQL.
I'm looking for a function to calculate business hours in seconds based on my calendar table. In my calendar table I have 2 columns in it. 1st column is date and opening time and 2nd column date and end time.
I tried the solution from #Ezlo SQL Server counting hours between dates excluding Fri 6pm - Mon 6am
In his solution when its the same date it doubles the time for example the output has to be 75 secs its then 150 secs. I want to be able to call the function in a query like WorkTime (#StartDate DATETIME, #FinishDate DATETIME) while it passes through my calendar table. The startdate and finishdate has to be anything I put as value in it ie a columns (datecreated,dateclosed) with dates.
Ie: a query with 1000 rows like this format.
Scenario 1
TicketID: 111111
DateCreated: 2019-01-01 10:00:52
DateClosed: 2019-01-02 08:35:00
Function result has to be 300 secs while it checked my calendar table.
Scenario 2
TicketID: 111112
DateCreated: 2019-01-02 16:30:00
DateClosed: 2019-01-02 16:15:00
Function result has to be 900 secs while it checked my calendar table.
Scenario 3
TicketID: 111113
DateCreated: 2019-01-02 20:00:00
DateClosed: 2019-01-03 09:30:00
Function result: 3600 secs
Scenario 4
TicketID: 111114
DateCreated: 2019-01-05 20:00:00
DateClosed: 2019-01-07 09:00:00
Function result: 1800 secs
Calendar table
As you can see I have ie 1st of January set to 08:30 so it doesn't calculate the time (Holiday). And so I have a set of Holidays set the same way.
Weekends are left out see calendar table, in that way it is excluded and the time starts to count on the first business day.
I have tried multiple times but with no success of getting it to work as I wish.
Hopefully you gurus can me help me to achieve this.
After days of searching this forum. The answer I was looking for can be found here.
Calculate time difference (only working hours) in minutes between two dates
I am making attendance system. I choose IN time for user for particular date using minimum time for the date and time greater than 4.00 AM. Now I want to find OUT time of user based on condition. Now OUT time can span across days. (because people work in night shift.). So condition for OUT time is maximum time of selected IN time plus 16 hours.
How can i write sql query for this?
My Database looks like this..
Name Time In-0/Out-1
Ajay 1.00 AM 4/12/2012 0
Ajay 6.00 AM 4/12/2012 1
Ajay 9.00 PM 4/12/2012 0 in time
Ajay 2.00 AM 5/12/2012 1
Ajay 2.15 AM 5/12/2012 0
Ajay 6.10 AM 5/12/2012 1 out time
I am fetching IN and OUT time for user. Some people work in night shift. So IN time is fetched as min of today's IN time which is greater than 4 AM. And OUT time needs to be fetched as Max time of today's out time which is less than of 16 hour added to today's IN time.
I am designing a SQL table to store hours of operation for stores.
Some stores have very simple hours: Monday to Sunday from 9:30AM to 10:00PM
Others are little more complicated. Please consider the following scenario:
Monday: Open All Day
Tuesday: 7:30AM – 2:30PM & 4:15PM – 11:00 PM
Wednesday: 7:00PM – 12:30 AM (technically closing on Thursday morning)
Thursday: 9:00AM – 6:00PM
Friday: closed.
How would you design the table(s)?
EDIT
The hours will be used to showing if a store is open at a user selected time.
A different table can probably handle any exceptions, such as holidays.
The store hours will not change from week to week.
A table like this would be easy for both the output you posted, as well as just firing a bit back (open? yes/no):
Store | Day | Open | Closed
---------------------------
1 | 1 | 0000 | 2400
1 | 2 | 0730 | 1430
1 | 2 | 1615 | 2300
...
Features:
Using 24-hour isn't necessary, but makes math easier.
Store ID would presumably join to a lookup table where you stored Store information
Day ID would translate to day of week (1 = Sunday, 2 = Monday, etc.)
To query for your dataset, just:
SELECT Day, Open, Close... (you'd want to format Open/Close obviously)
To query IsOpen?, just:
SELECT CASE WHEN #desiredtime BETWEEN Open AND Closed THEN 1 ELSE 0 END
FROM table
WHERE store = #Store
Think of it more as defining time frames, days / weeks are more complex, because they have rules and defined start and stops.
How would you define a timeframe?
one constraint (Start[Time and Day]), one reference 'Duration' (hours, minutes,.. of the span)*. Now the shifts (timeframes) can span multiple days and you don't have to work complex logic to extract and use the data in calculations.
**Store_Hours**
Store | Day | Open | DURATION
---------------------------
1 | 1 | 0000 | 24
1 | 2 | 0730 | 7
1 | 2 | 1615 | 6.75
...
1 | 3 | 1900 | 5.5
Do you have to do more than just store and display it?
I think a design which needs to tell if a store is open at a particular time would have to be informed by all of the possibilities, otherwise, you will end up not being able to accommodate something.
What about holiday exceptions?
I would consider storing them as intervals based on a base time (minutes since time 0 on a week).
So 0 is midnight on Monday.
Interval 1 would be 0 - 1440
Interval 2 would be 1890 - 2310
etc.
You could easily convert a user selected time into a minute offset and determine if a store was open.
Your only problem remaining would be interpretation in display for friendly display (probably some extensive logic, but not impossible) and overlap at time 10080 -> 0.