Duration of each status PostgreSQL - postgresql

I have a table that has a couple of statuses for each ticket - e.g:
OPENED
CLOSED
RE-OPENED
FINISHED
REJECTED
...
And each row in the status table has the timestamp of when a status was set / changed, something like this:
time
status
2021-11-22 09:40
OPENED
2021-11-22 09:50
CLOSED
2021-11-22 10:10
RE-OPENED
2021-11-22 10:30
FINISHED
2021-11-22 10:50
CLOSED
2021-11-22 11:30
RE-OPENED
2021-11-22 12:10
REJECTED
What I am interested in, is how to calculate the duration of each status in a time range?
Time range can be anything from one hour to a couple of days. Let's use an example and try to calculate the duration for each status between
2021-11-22 10:00 - 2021-11-22 11:00
If we take a look into the table:
time
status
2021-11-22 09:50
CLOSED
2021-11-22 10:10
RE-OPENED
2021-11-22 10:30
FINISHED
2021-11-22 10:50
CLOSED
2021-11-22 11:30
RE-OPENED
10:00 - 10:10 --> CLOSED
10:10 - 10:30 --> RE-OPENED
10:30 - 10:50 --> FINISHED
10:50 - 11:00 --> CLOSED
The result table would look something like this:
status
duration
OPENED
00:00
CLOSED
00:20
RE-OPENED
00:20
REJECTED
00:00
FINISHED
00:20
P.S. As you can see, even though the first appearance of a status entry in the time range (10:00 - 11:00) was at 10:10, we have to include last reported status before the selected/desired time-range.
I appreciate the help.

You can use LEAD() to calculate time difference for rows which meet the time range condition. Then sum the time difference as duration. This is helpful when your time range spans a couple of hours or days.
WITH calc_table AS (
SELECT *, LEAD(CASE WHEN t1.time between
'2021-11-22 10:00' AND '2021-11-22 11:00'
THEN t1.time END) OVER (Order by t1.time) - t1.time as timediff
FROM mytable t1
)
SELECT DISTINCT c.status,
SUM(CASE WHEN c.timediff IS NOT NULL THEN c.timediff ELSE '00:00:00' END) AS duration
FROM calc_table c
GROUP BY c.status;
See the Demo

Related

Azure Data Factory - Triggers

I want to achieve below requirement -
Say I have a trigger named RunBatchJob
Weekdays: Monday - Friday
I want to schedule this trigger to run on below times:
Recurrence -> Every 5 Minutes between 06:00AM until 10:00 PM
Recurrence -> Every 30 Minutes between 10:01 PM until 05:59 AM
Weekends: Saturday, Sunday
I want to schedule this trigger to run on below times:
Recurrence -> Every 10 Minutes between 06:00AM until 10:00 PM
Recurrence -> Every 1 Hour between 10:01 PM until 05:59 AM
This used to be particularly easy on SQL server jobs, can anyone please advise me how to do it on ADF?
You can separate the triggers into 4 cases. Use a scheduled trigger for 1 week occurrence. Then you can enumerate all the combinations of trigger times in each trigger.
Here is the example for the 1st case(Mon-Fri Every 5 Minutes between 06:00 AM until 10:00 PM)
You can just repeat the idea for 2nd case(Mon-Fri Every 30 Minutes between 10:01 PM until 05:59 PM)

Truncate date by month with custom start day in postgres

I am getting some statistics using a query like
SELECT date_trunc('month', created_at) AS time, count(DISTINCT "user_id") AS mau
FROM "session"
GROUP BY time
ORDER BY time;
Which is working fine if I want to get monthly active users for each calendar month. But I would like to shift the result to show last X moths starting from today instead of actual calendar months. How do I do? Can I add an offset in some way?
EDIT
As an example, I am currently getting results like
time | mau
2022-04-01 | 10
2022-05-01 | 20
2022-06-01 | 30
But I would like it to be something like (where 2022-06-07 is today)
time | mau
2022-04-07 | 10
2022-05-07 | 20
2022-06-07 | 30

Check opening hours in different timezones

My Specs:
Postgres 9.6.6, latest Ubuntu LTS
Server Timezone is GMT
A table with two columns that shows store opening and closing times, with each timezone.
Here's the table:
ShopId OpenAt CloseAt
1 09:00:00 -08 17:00:00 -08
2 09:30:00 -05 17:30:00 -05
3 08:00:00 -11 15:00:00 -11
4 10:00:00 +07 15:30:00 +07
What I need to know is if at moment (at my current GMT time), the shop is open. Taking into consideration that Saturday and Sunday it's closed.
I'm digging around and I got something like:
SELECT ((OpenAt,CloseAt) OVERLAPS(NOW())) AND ISODOW < 6
with no luck...
Thanks
Perez
Try this :
SELECT ((date_trunc('day',nowAtShopLocation)+"OpenAt"::time, date_trunc('day',nowAtShopLocation)+"CloseAt"::time) OVERLAPS(nowAtShopLocation,nowAtShopLocation)) and EXTRACT (ISODOW FROM nowAtShopLocation) <6
from (
select *,now() AT TIME ZONE 'UTC'+(EXTRACT(TIMEZONE_HOUR FROM "OpenAt")||' hour')::interval nowAtShopLocation from your_table
) a

RUN Task Scheduler in different times

Is it possible to create TS (Windows Server 2012 R2) for below timings in everyday?
9:00, 15:00, 19:00 and 3:00
I have idea to create TS for every 6 hours in a day, but i have a 4 hours gap between 15:00 and 19:00.
any idea?
~~Ramesh

phpScheduleIt / Booked Scheduler - Time slots layout issue

Hoping someone can shed some light on this.
I'm trying to setup a schedule with 15 minute time slots (or any time slots for that matter).
Whenever I try and insert the following slot configuration I'm presented with this warning - 'Slots must be provided for all 24 hours of the day beginning and ending at 12:00 AM.'
Time slots:
Reservable:
07:00 - 07:30
07:30 - 08:00
08:00 - 08:30
08:30 - 09:00
09:00 - 09:30
09:30 - 10:00
10:00 - 10:30
10:30 - 11:00
11:00 - 11:30
11:30 - 12:00
12:00 - 12:30
12:30 - 13:00
13:00 - 13:30
13:30 - 14:00
14:00 - 14:30
14:30 - 15:00
15:00 - 15:30
15:30 - 16:00
16:00 - 16:30
16:30 - 17:00
17:00 - 17:30
17:30 - 18:00
18:00 - 18:30
18:30 - 19:00
19:00 - 19:30
19:30 - 20:00
20:00 - 20:30
Blocked
06:30 - 07:00
20:30 - 21:00
Any input will be appreciated.
Thank you
looks like you missed a slot. You need to cover all 24 hours. In blocked you would need to enter 20:30 - 07:00 or if that doesn't work, try 20:30 - 00:00 and 00:00 - 07:00.
Good luck!
Judou !
ooks like you missed a slot. You need to cover all 24 hours. In blocked you would need to enter 20:30 - 07:00 or if that doesn't work, try 20:30 - 00:00 and 00:00 - 07:00.