Sql query where condition based on fetched column value - sql

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.

Related

Timeseries : date time averaging and abnormally detection

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

Calculate total manufacturing output over a shift for each location

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?

Function for business hours in seconds based on calendar table

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

pick up date within some range

I have table like this. This is a record of consultant done activity for aftercare when some new candidate starts a job.I have built a report to compare if aftercare is done on time or not. in the report it start showing 3 days ago that aftercare is due on this date. But some consultants do aftercare before or after and also on date which is correct. I am fetching MIN aftercaredone date to check and compare with the aftercare due date and shows in the report if it is correct like below.
MIN(case when DESCRIPTION='Aftercare Pre Start' then DUEDATECONCAND end) DUEDATEPreStart hin
But my min date logic fails here. Actually I like to pick up all aftercaredone dates and compare with aftercareduedate . If any date falls within required period then should show report result correct otherwise fails. I mean if someone did aftercare within 3 days difference from AFtercareduedate then should show correct or otherwise in query it should show N(means not done). And also I need to exclude weekends.
ConsultantID CandiateID candidateNAME AfterCareDueDate AftercareDone
123 1 Bob 01/02/2019 20/02/2019
123 1 Bob 01/02/2019 01/02/2019
123 1 Bob 01/02/2019 07/02/2019
100 2 Rob 01/02/2019 01/02/2019
100 2 Rob 01/02/2019 10/02/2019
200 3 ABC 01/02/2019 20/01/2019

Time Difference between 2 datetime fileds, only working hours

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