Sum values of stop time into calendar - sql

I'm trying to create a stop report for my plant while learning SQL.
The goal is to get a report containing the amount of minutes stopped planned and unplanned and the amount of stops planned and unplanned. All of this should be reported by day.
Currently I have these values:
Datum: Date
Unplanned: The amount of minutes our plant stopped that day when it was not planned
#unpplanned: the amount of stops we had that day that were unplanned
Daysunplanned: The amount of days the unplanned stop lasted
planned: The amount of minutes our plant stopped that day when it was planned
#planned: The amount of stops we had that day that were planned
dayspl: The amount of days the planned stop lasted
nextday: The amount of minutes that the stop took that should be counted to the next day. A stop can last from 20:00 7/02/2023 till 05:00 8/02/2023).
Can't embed yet :/
My issue is the following:
On 2023-02-06 the plant stopped 1130 minutes unplanned. The stop lasted till 07-02-2023. You can see this because dayunpla = 1.
So the results should be:
2023-02-07 1378
2023-02-06 1130
If dayunpla = 4 we should get:
2023-02-11 1378
2023-02-10 1440
2023-02-09 1440
2023-02-08 1440
2023-02-06 1130
1440 is 24 hours.
I can't think of a way to get from my current table to what I want.
I tried all google searches I could think of but did not find anything helpfull.
Is there a way to fix this using just SQL?
Thanks for your time!
Tried google and a lot of tinkering

Related

Date_diff with specific condition time start and time end

is it possible to have date_diff with specific start and end time?
let say my store are open from 8AM - 10PM, which is 14 Hours.
and I have a lot of stuff to sell during that time. One of the SKU is out of stock from 2022-11-01 06.00 PM until tomorrow 2022-11-02 11.00 AM.
Instead of calculate 24 hours, I just want to calculate only from opening store until it closed or until its restock. Meaning from 6PM to 11AM is 8 Hours
my query
select date_diff('2022-11-02 11.00 AM', '2022-11-02 06.00 PM', hour) from table
with the result 17 hours instead of 8 hours
There isn't a way to configure DATE_DIFF to do this for you, but it's possible to do what you want, with some effort.
You should convert your dates to timestamps (TIMESTAMP(yourdate) or CAST(yourdate AS TIMESTAMP)) and use TIMESTAMP_DIFF instead.
This will allow you to work with smaller intervals than days.
For your calculation, you ultimately need to find the total time difference between the two timestamps and then subtract the out-of-hours timeframe.
However, calculating the latter is not as simple as taking the difference in days and multiplying by 8 hours (10pm-6am), because your out-of-hours calculation has to account for weekends and possibly holidays etc. Hence it can get quite complex, which is where the solution in my first link might come in.

SQL query for calculating number of days and nights

I am looking for a query that tells number of days and nights while travelling
my day time ends at 18:00 and after that night time starts
night time ends at 09:00
query that tells how many days and night a person has traveled

Subtracting two dates (including hours and minutes) considering only working time vba or Excel

I need to subtract two dates (including hours and minutes), but I only need to consider working hours. That is, I need to omit lunch time (from 13 to 14 hrs), weekends and hours after 18 hrs and before 9 hrs of the following day, in a working day (from Mo to Fr). Any thoughts?
I don't mind if it's an Excel formula or a vba code.
I have this formula, but it doesn't omit lunch time:
9*(NETWORKDAYS(initial_time;ending_time)-1)-24*((MOD(initial_time;1)-MOD(ending_‌​time;1)))
Here's a possible solution. It assumes an 8 hour work days for all but the start and end date. Also that start date/time is 9:00 or after and end date/time is 18:00 or earlier and that both are on a weekday.
=(NETWORKDAYS(A2,B2)-2)*8+IF(MOD(A2,1)>0.58333,(TIME(18,0,0)-MOD(A2,1))*24,(TIME(18,0,0)-MOD(A2,1))*24-1)+IF(MOD(B2,1)>0.58333,(MOD(B2,1)-TIME(9,0,0))*24-1,(MOD(B2,1)-TIME(9,0,0))*24)
.58333 equates to 14:00. The formula:
multiplies networkdays * 8
+ hours from start date/time until 18:00 subtracting 1 hour if start time is before 14:00
+ hours from 9:00 until end date/time subtracting 1 hour if end time is after 14:00
Of course this doesn't take any holidays into account.

How to add working hours depending on weekday in sql

I want to create a basic case logging system and when somebody opens a new issue, the issue is assigned a Sr_number with a given number of hours. For example Sr_number 1 is 4 hours, 2 is 6 hours, 3 is 8 hours and 4 is 24 hours.
Now adding hours onto a time stamp is easy but the catch is I need to take into account working hours which are 09:00 to 17:00 Monday to Friday.So if a case is given a 12 hour Sr_number and the deadline for this falls at 16:00 on a week day then the deadline is extended to the next working day. Basically the deadline is 12 working hours.And calculation should be 1 hour worked for the issue logged on same day and remaining 11 hours to next working day.
If in case it is sun, it should consider directly go to monday.
Example:
Case created on: 10/06/2015 12:04:39 PM- with Sr_number 1 (12 Hours) Deadline is now: 10/07/2015 12.05 PM
Make sense?
Another catch is I need to take into account hours On Hold and these two have to be only within working hours.
For some case,saturdays is working ,for some its holiday.
How should i proceed.
I tried performing datepart,dateadd and datediff functions.But i could find only weekday.
I am new at sql.Please guide for the same

Sql server agent doesn't run as scheduled(Recurring)

I have a SQL job set up to run once every 12 hours everyday -at 7AM and 7PM. The job only runs at 7PM everyday but not the next day morning.
Can you help me and point out the problem please? Thanks!
Schedule Type :Recurring. Enabled.
Frequency-
occurs: Daily
Recurs: 1 Day
Daily Frequency -
Occurs every:12 hours
Starting at 19:00:00
Ending at 20:59:59
Duration-
Start Date :15/11/2013
No End Date
This is what the summary at the end says :
Occurs every day every 12 hour(s) between 19:00:00 and 20:59:59. Schedule will be used starting on 2013-11-15.