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
Related
I am new to sql and this is my first ever question. I am working with a sample database that I want to extract specific information from to display as a dashboard. The issue is that I can do this partially but I cannot seem to figure it out properly.
``SELECT
S_date as date,
p_time as time,
process_id as process,
sc_gun as scannumb,
sum(line_qty) as linetotal,
sum(area_qty) as areatotal
FROM dbfile6
WHERE
process_id in('0010','0020','0030')
and sc_gun in = ('10','20','30','40','50')
and s_date = curdate() - 1 and p_time between '22:00:00' and '23:59:59'
or s_date = curdate() and p_time between '00:00:00' and '06:00:00'
GROUP BY p_time, s_date, process_id, sc_gun
ORDER BY s_date, process_id
What do I want to display?
I can do partially where I want it to work to yesterdays date (s_date) recurring but I want this to only happen Monday to Friday, skipping the weekend so when we are on Monday, it looks at Fridays data from the database.
I want to show the time as a range, a night range. The range is 20:00:00 - 06:00:00. The range is tricky as it crosses over to the next day, this could work for Monday to Thursday but not Friday as there is no working weekend so what would I do here? In addition to this, I can sum up the values successfully and display it as averages for each process but then once I add the time in, it displays each result individually.
The table below is what it looks like in the database, however as mentioned earlier, the desired result is for each process to have the line_qty and area_qty summed up by time range and a day and night cycle.
s_date
p_time
process_id
sc_gun
line_qty
area_qty
04/05/2022
04:49:52
0010
10
2
12
03/05/2022
11:50:00
0010
10
5
14
03/05/2022
19:50:00
0010
10
7
16
03/05/2022
13:50:00
0020
20
4
6
03/05/2022
19:50:00
0010
10
7
16
The below gives me week numbers where week 1 starts on 1/4/2021
date_trunc('week', transaction_date) as week_number
How can I create a week_number where the week starts on January 1st and counts up 7 days for every week thereafter (for every year)?
And round up/down to 52 weeks at the end of the year?
Code attempted:
This doesn't give me the answer, but I'm thinking something like this might work...
ceil(extract(day from transaction_date)/7) as week_number
Expected Output:
transaction_date
week_number
1/1/2020
1
1/8/2020
2
...
...
12/31/2020
52
1/1/2021
1
1/8/2021
2
...
...
12/27/2021
52
12/28/2021
52
12/29/2021
52
12/30/2021
52
12/31/2021
52
1/1/2022
1
Thanks in advance!
A simple way is to use date arithmetic:
select 1 + (transaction_date - date_trunc('year', transaction_date)) / 7 as year_week
The below gives me week numbers where week 1 starts on 1/4/2021
It is the default behaviour and it is defined that way in ISO.
WEEK_OF_YEAR_POLICY
Type Session — Can be set for Account » User » Session
Description
Specifies how the weeks in a given year are computed.
Values
0: The semantics used are equivalent to the ISO semantics, in which a week belongs to a given year if at least 4 days of that week are in that year.
1: January 1 is included in the first week of the year and December 31 is included in the last week of the year.
Default 0 (i.e. ISO-like behavior)
It could be overrriden on multiple levels. The most granular is on the session level:
ALTER SESSION SET WEEK_OF_YEAR_POLICY = 1;
Then you could use the standard code:
SELECT date_trunc('week', transaction_date) as week_number
FROM ...;
please help me with a query to find running Avg for every 15 days. I have used below query but not sure how to display only 15 days Avg.
Select Date,
Avg(Qty) OVER (ORDER BY Date ROWS BETWEEN 15 PRECEDING AND CURRENT ROW) AS RunningAvg
FROM Sample
Sample Table : (Contains Qty for each Day)
Date Qty
2014-10-01 4
2014-10-02 5
..
..
2014-12-31 4
Expected Result.
Date RunningAvg
2014-10-01 4
2014-10-15 XX
2014-11-01 XX
2014-11-15 XX
2014-12-01 XX
.
.
.
I'm a bit baffled by the question. Your results seem to suggest that you want the values on the 1st and 15th of the month -- and that has nothing to do with 15-day moving averages. For such filtering you can use:
select t.*
from t
where day(date) in (1, 15);
As you know, some months have 28, 29, or 31 days so "15 days" has nothing to do with the day of the months. And the number of days between the 1st and 15th is 14 days, not 15.
I have the below set of data which represents employee sick/absence days over a period (12 months) of time, in a table named Absence:
Day Date DaysSick OccasionsSick Notes
Tuesday 2016-09-27 1 Lisa A working today
Thursday 2016-09-29 1 Lisa sick today Celeste
Thursday 2017-01-05 1 Lisa sick today
I would like to update the OccasionsSick column based upon the instances of being sick. So i would have the following:
Day Date DaysSick OccasionsSick Notes
Tuesday 2016-09-27 1 1 Lisa A working today
Thursday 2016-09-29 1 Lisa sick today Celeste
Thursday 2017-01-05 1 1 Lisa sick today
So, the first two entries are the same period of sick leave, so i need a 1 in the first row, and the last entry is a separate sick period, so a 1 again.
Now, in order to establish a sick period there would be a reference to a roster table containing the below:
Date RosterType
2016-09-27 Sick
2016-09-28 Day Off
2016-09-29 Sick
2016-09-30 Normal
So the 27th and 29th were sick days, but the 28th was a standard day off, which is a likely occurrence, so using consecutive days is not an option. I need to be able to look for sick days until a "normal" RosterType is found, this then breaks the sick period. This 1 then needs to be assigned as per the desired result set.
What is the best way of updating the data here? I have come up with a big blank on this, apart from the logic of determining a sick period.
I am presenting this data in Excel with VBA, so it could also be easier to assign the sick periods in VBA, as opposed to SQL for the raw data
Please check this out.
This assumes that there is an entry in the roster for each day.
Basically I'm just building a period and counting the days in the roster.
If there are normal days in between it counts as a new period.
WITH CTE AS (
SELECT
[day]
,[date]
,LAG(date, 1) over (order by date) datebefore
,[dayssick]
FROM [dbo].[absence]
)
SELECT
*
,CASE WHEN ((SELECT COUNT(1) FROM [dbo].[rostertype] WHERE date < c.date AND date > c.datebefore AND rostertype = 'Normal') > 0
OR c.datebefore IS NULL) THEN 1 ELSE 0 END OccasionsSick
FROM CTE c
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