So, the table setup is something like this:
table: ticket(ticket_id, ...)
table: ticket_status_history(status_history_id, ticket_id, ticket_status, ticket_status_datetime)
The default ticket_status is OPEN
, and the first ticket status that I'm interested in is ACKNOWLEDGED
.
So, the idea is that a specific ticket has a set of ticket_status_history
events, each recorded in the separate table. Each ticket status entry points to its corresponding ticket, ticket_id is a foreign key.
Now, a ticket can actually be created directly in ACKNOWLEDGED
so it would get a corresponding entry directly in ACKNOWLEDGED
, without ever being in OPEN
. But many of them will go OPEN
-> ACKNOWLEDGED
-> ...
What I'd like to do would be to determine for each ticket the time interval between ticket creation (OPEN
) and ticket acknowledgment (ACKNOWLEDGE
), but if the state is directly ACKNOWLEDGE
, set the time difference as a default of 0 (because the ticket was created directly in this state).
Is this doable in SQL, for PostgreSQL? I'm a bit stumped at the moment. I found this: Calculate Time Difference Between Two Rows but it's for SQL Server, instead, plus I'm not sure how the default value could be included.
The end state would actually be aggregating the time differences and computing an average duration, but I can't figure out the first step