简体   繁体   中英

Average time between two datetime columns in different tables

I have two tables, one has the request submission date while the other has request approved date.

I need to calculate the average time taken to approve the request that is raised in this week, requests raised this month, requests raised this year for analytics data.

How can we calculate the average time taken to approve the request?

We also need to take care of the scenario for requests that is raised this week or this month, which is not yet approved when calculating the average.

Below is the table structure which depicts the time when the request is submitted and the time when the request got approved.task_date is the date that gets updated on the change of the request status.

Table #1: Request_details

id           request_submission_date
----------------------------------------
S2021000001  2022-01-01 11:32:00.0000000
S2021000002  2022-01-02 11:50:00.0000000
S2021000003  2022-01-03 15:01:00.0000000

Table #2: request_status

    task_date               task_name     request_id
------------------------------------------------------
2022-01-01 11:42:00.0000000   approved    S2021000001
2022-01-02 11:55:00.0000000   approved    S2021000002         
2022-01-03 15:02:00.0000000   pending     S2021000003 

Expected result Avg time for Req raised this week

S2021000001 took 10 minutes to get approved S2021000002 took 5 minutes to get approved S2021000003 is still in pending state so we can consider it as 0. ((10 + 5 + 0) / 3) = average is (3 mins)

I tried the following query but it's not giving the expected output

 SELECT AVG(datediff(minute, request_submission_date, task_date)) as 
 AverageTime FROM request_details r, request_status ws where r.id = 
 ws.request_id and r.request_submission_date IS NOT NULL and 
 ws.task_name in('Approved')
 and  r.request_submission_date between '2022-01-01' and '2022-01-05'           
 group by Convert(date,r.request_submission_date);
 

Don't use the old style to join the table FROM request_details r, request_status ws . Use the ANSI join syntax as show below

Also you don't want the GROUP BY Convert(date,r.request_submission_date) unless you want the AVG by date.

Since your request_submission_date contain the time component. Using BETWEEN is actually excluded rows with date 2022-01-05 after midnight. It is easier to use the >= and < operator.

 SELECT AVG(datediff(minute, r.request_submission_date, ws.task_date)) as AverageTime 
 FROM       request_details r
 INNER JOIN request_status ws 
         ON r.id =  ws.request_id 
 WHERE  r.request_submission_date IS NOT NULL 
 AND    ws.task_name in('Approved')
 AND    r.request_submission_date >= '2022-01-01' 
 AND    r.request_submission_date <  '2022-01-06'

Maybe it's just a matter of by what you group on.

Fe if you group by year & week

SELECT CONCAT(DATEPART(YEAR, detail.request_submission_date), '-', FORMAT(DATEPART(WEEK, detail.request_submission_date), '00')) AS [Submission_yearweek], AVG(DATEDIFF(MINUTE, detail.request_submission_date, status.task_date)) as AverageTime FROM request_details AS detail JOIN request_status AS status ON status.request_id = detail.id AND status.task_name IN ('approved') WHERE detail.request_submission_date >= '2022-01-01 00:00:00' AND detail.request_submission_date < '2022-02-01 00:00:00' GROUP BY DATEPART(YEAR, detail.request_submission_date), DATEPART(WEEK, detail.request_submission_date);
Submission_yearweek AverageTime
2022-01 10
2022-02 5

db<>fiddle here

I don't know the whole schema of your db and tables, so I just used only three fields from you question for each table and populated it with your test data. Also the average for this week is (10 + 5 + 0) / 3 = 5, not 3.

create table Request_details
(
    id varchar(11) not null,
    request_submission_date datetime not null
)

create table request_status
(
    task_date datetime not null,
    task_name varchar(50) not null,
    request_id varchar(11) not null
)

insert into Request_details 
 values ('S2021000001', '2022-01-01 11:32:00.00'),
('S2021000002',  '2022-01-02 11:50:00.00'),
('S2021000003' , '2022-01-03 15:01:00.00')


insert into request_status 
values 
('2022-01-01 11:42:00.00',   'approved',    'S2021000001'),
('2022-01-02 11:55:00.00',   'approved',    'S2021000002'),      
('2022-01-03 15:02:00.000',   'pending',     'S2021000003')

-- if request status is pending, we treat it as 0, otherwise datediff
select AVG(iif(rs.task_name = 'pending', 0, DATEDIFF(minute,rd.request_submission_date, rs.task_date))) as Average
from Request_details as rd
inner join request_status as rs
on rd.id = rs.request_id
where rd.request_submission_date >= '2022-01-01' and rd.request_submission_date < '2022-01-06';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM