Split a row in sql server - sql

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

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

How to update existing rows based in next month on current month data in same SQL table?

I have one SQL table called Employee_invoice
JobSite
Name
InvocieDate
InvoiceAmount
Crew_size
J003297
David
3/1/2022
1600
10
J003297
Jhon
5/1/2022
2000
20
J102899
Andy
12/1/2022
4000
30
J003297
Olive
22/1/2022
2000
45
J003297
David
3/2/2022
0
0
J003297
Jhon
5/2/2022
0
0
J102899
Andy
12/2/2022
0
0
J003297
Olive
22/2/2022
0
0
So I want to update InvoiceAmount, Crew_size column of next month i.e. February based Previous month i.e. January month. The join condition will be matched on JobSite and Name column.
I want a stored procedure for this, which will run 1st day of every month and update multiple columns. (#Tabletype)
The result table should look like this:

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?

Add a new dataframe column which counts the values in certain column less than the date prior to the time

EDITED
I want to add a new column called prev_message_left which counts the no. of messages_left per ID less than the date prior the given time. Basically I want to have a column which says how many times we had left message on call to that customer prior to the current time and date. This is how my data frame looks like
date ID call_time message_left
20191101 1 8:00 0
20191102 2 9:00 1
20191030 1 16:00 1
20191103 2 10:30 1
20191105 2 14:00 0
20191030 1 15:30 0
I want to add an additional column called prev_message_left_count
date ID call_time message_left prev_message_left_count
20191101 1 8:00 0 1
20191102 2 9:00 1 0
20191030 1 16:00 1 0
20191103 2 10:30 1 1
20191105 2 14:00 0 2
20191030 1 15:30 0 0
My dataframe has 15 columns and 90k rows.
I have various other columns in this dataframe and there are columns like 'No Message Left', 'Responded' for which I will have to compute additional columns called 'Previous_no_message_left' and 'prev_responded' similar to 'prev_message_left'
Use DataFrame.sort_values to get the cumulative sum in the correct order by groups. You can create groups using DataFrame.groupby:
df['prev_message_left_count']= (df.sort_values(['date','call_time'])
.groupby('ID')['message_left']
.apply(lambda x: x.shift(fill_value=0)
.cumsum()) )
print(df)
date ID call_time message_left prev_message_left_count
0 20191101 1 8:00 0 1
1 20191102 2 9:00 1 0
2 20191030 1 16:00 1 0
3 20191103 2 10:30 1 1
4 20191105 2 14:00 0 2
5 20191030 1 15:30 0 0
sometimes GroupBy.apply is slow so it may be advisable
df['prev_message_left_count']=( df.sort_values(['date','call_time'])
.groupby('ID')
.shift(fill_value=0)
.groupby(df['ID'])['message_left']
.cumsum()

How to display data row wise for every day per week in sql server

i have a table in which userid, clockInTime , clockOutTime and ClockDate are stored like below
ID BId ClockInDateTime ClockOutDateTime ClockDate UserId
6 1 2013-09-06 03:46:00.000 2013-09-06 05:46:15.000 1 2013-09-06 05:46:46.657 1
7 1 2013-09-06 01:50:19.480 2013-09-06 05:50:26.383 1 2013-09-06 05:50:19.480 2
8 1 2013-09-07 02:08:17.253 2013-09-07 06:08:21.153 1 2013-09-07 06:08:17.253 1
9 1 2013-09-07 03:46:10.300 2013-09-07 07:46:14.827 2 2013-09-07 07:46:10.300 2
10 1 2013-09-06 03:48:16.807 2013-09-06 07:48:19.967 2 2013-09-06 07:48:16.807 3
now i want to display data asw below per user
Name Mon Tue Wed Thus Fri Sat Sun
N. Patel 8:00:40 9:00:40 10:00:40 11:00:40 12:00:40 13:00:40 14:00:40
M. Bruell 8:00:40 9:00:40 10:00:40 11:00:40 12:00:40 13:00:40 14:00:40
here i want record day wise per user in single row, is there any query without using cursor? from userid i will get username but for every day there are new record for new day, but for week i want that record in single row day wise, and daily working hours for each day by ClokInDateTime and ClockOutDateTime, and if Shift is between 9:00 P.M to 6:00 A.M then first 3 hours should be display on that day and other 6 hour should be display in next day,