Calculate total manufacturing output over a shift for each location - sql

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?

Related

Splunk: Split a time period into hourly intervals

index="dummy" url="https://www.dummy.com" status="200 OK"
| stats count by id
| where count > 10
If I apply this above query for 1 day, I would get, for example
id count
ABC 50
XYZ 60
..
This would mean ABC hit https://www.dummy.com 50 times in 1 day, and XYZ called that 60 times.
Now I want to check this for 1 day but with every two hours interval
Suppose, ABC called that request 25 times at 12:00 AM, then 25 times at 3:AM,
and XYZ called all the 60 requests between 12 AM and 2 AM
I want the output to look like this (time format doesn't matter)
id count time
XYZ 60 12:00 AM
ABC 25 12:00 AM
ABC 25 2:00 AM
..
You can use bin to group events into time buckets. You can use any span value, but for the 2 hours you mentioned, the updated query would be:
index="dummy" url="https://www.dummy.com" status="200 OK"
| bin _time span=2h
| stats count by id, _time
| where count > 10

Split a row in sql server

In my DB, there are 2 tables ie RateCalendar & Blackout.
Ratecalendar stores daily, weekly, etc. rates between 2 dates for a Car Class. eg
From 10Jan to 27 May 2016, Daily rates are say £20,
weekly rates are £50 and so on.
Similarly, for other Dates range daily weekly rates are specified.
Suppose for 2 days (4May to 5May) I blackout my car ie car won't be available for a location. Now, I have to show the record as splitted ie from 10Jan to 3May, Cars will be available ie IsAvailable->True.
From 4th May to 5th May as Blackout (ie IsAvailable->False) and
from 6th till 29May again cars will be available.
All records will not split. Only those Cars whose details are present in blackout will split.
Moreover, depending on Blackout dates, splitting will be done. For eg. in blackout if there are two records for a car ie car is blacked out for say 10Jan to 12Jan And 4th May to 5th May, then, output will be like:
Car StartDate EndDate Available
Car1 10Jan2016 12:00 12Jan2016 1:00 N
Car1 12Jan2016 1:00 4May2016 9:00 Y
Car1 4May2016 9:00 5May2016 12:00 N
Car1 5May2016 12:00 27May2016 12:00 Y
Car2 10Jan2016 10:00 27May2016 12:00 Y
So, row can be splitted into 0 or 2 or multiple depending on Blackout dates for car.
What's the best way to do this?
Below is the layout of
1) RateCalendar Table(there are 2 more fields AgencyId,LocationId as shown in Blackout Table)
RateCalId RateGroupId CarClassId StartDate EndDate DailyRate
1 1 1 10Jan2016 10:00 26May2016 11:00 20
2 1 1 27May2016 11:00 28June2016 12:00 40
2) Blackout Table
BlackoutId AgencyId LocationId BeginDate EndDate
1 1 1 10Jan2016 12:00 12Jan2016 1:00
2 1 1 4May2016 9:00 5May2016 12:00
3) BlackoutCars
BlackoutId CarId
1 1
1 2
2 1
2 2
4) BlackoutRateGroup
BlackoutId RategroupId
1 1
1 0(0=>all)
2 3

Find same dates in a column and count numbers from there rows together

So I have a list with datetimes, mostly there is the same datetimes twice. In the same row of the Dates there is a number which should be summed up and listed in another sheet.
List example (What I Have now):
1 07.09.2015 09:00 118
2 07.09.2015 09:00 31
3 07.09.2015 08:00 148
4 07.09.2015 08:00 56
5 07.09.2015 07:00 128
6 07.09.2015 07:00 49
The List could/should goes like this 24 hours a day 365 times a year, actually its just 3 months at the moment.
And I only want to have one Datetime with one number as sum behind.
It also could be posible that there is the same datetime more than twice
So it should look like this in the desired sheet:
1 07.09.2015 09:00 149
2 07.09.2015 08:00 204
3 07.09.2015 07:00 177
I'm not the best in excel VBA and i only can write a bit of code....
Use a pivot table.
Select all your data. (make sure you have header column and if not add)
select the insert Ribbon and - Insert -> pivot table.
Drag your the date to the rpws and the values as sum the values.
You can refrsh the table each time you add data to the data table by choosing Analyze ribbon -> refresh.
If you want to macro this process , the most simple way for starters is to copy a macro. You can view the code later and improve it.

MS ACCESS – Return a daily count of booked resources within a date range

Please note: this is not for an Access project as such, but a legacy application that uses an Access database for its back end.
Setup
Part of the application is a kind of Gantt chart, fixed to single day columns, where each row represents a single resource. Resources are booked out for a range of days and a booking is for a single resource, so they cannot overlap on a row. The range of dates that is in view is user selectable, open ended, and can be changed by various methods, including horizontal scrolling using mouse or keyboard.
Problem
I've been tasked with adding a row to the top of the chart to indicate overall resource usage for each day. Of course that's trivially easy to do by simply querying for each day in the range separately, but unfortunately that is proving to be an expensive process and therefore slows down horizontal scrolling a lot. So I'm looking for a way to do it more efficiently, hopefully with fewer database reads.
Here is a highly simplified example of the bookings table:
booking_ID | start_Date | end_Date | resource_ID
----------- -------------- ------------- -------------
1 2014-07-17 2014-07-20 21
2 2014-08-24 2014-08-29 4
3 2014-08-26 2014-09-02 21
4 2014-08-28 2014-09-04 19
Ideally, I would like a single query that returns each day within the specified range, along with a count of how many bookings there are on those days. So querying the data above for 20 days from 2014-07-17 would produce this:
check_Date | resources_Used
----------- ---------------
2014-07-17 1
2014-07-18 1
2014-07-19 1
2014-07-20 1
2014-07-21 0
2014-07-22 0
2014-07-23 0
2014-08-24 1
2014-08-25 1
2014-08-26 2
2014-08-27 2
2014-08-28 3
2014-08-29 3
2014-08-30 2
2014-08-31 2
2014-09-01 2
2014-09-02 2
2014-09-03 1
2014-09-04 1
2014-09-05 0
I can get a list of dates in the range by using a table of integers (starting at 0), with this:
SELECT CDATE('2014-07-17') + ID AS check_Date FROM Integers WHERE ID < 20
And I can get the count of resources used for a single day with something like this:
SELECT COUNT(*) AS resources_Used
FROM booking
WHERE start_Date <= CDATE('2014-09-04')
AND end_Date >= CDATE('2014-09-04')
But I can't figure out how (or if) I can tie them both together to get the desired results. Is this even possible?
Create a table called "calendar" and put a list of dates into it covering the necessary timeframe. It just needs one column called check_date with one row for each date. Use Excel, start at whatever date and just drag down, then import into the new table.
After your calendar table is set up you can run the following:
select c.check_date, count(b.resource_id) as resources_used
from calendar c, bookings b
where c.check_date between b.start_date and b.end_date
group by c.check_date

Sql query where condition based on fetched column value

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.