Question

In: Computer Science

Provide sql query to calculate average time between when an order is made and when it...

Provide sql query to calculate average time between when an order is made and when it is shipped. Use Avg() function to calculate average time.

Solutions

Expert Solution

Hi,

Please find the below details to find our the Average Time

--Declare Table
DECLARE @FirstTable TABLE (
                                                        ID INT, 
                                                        OrderDate datetime, 
                                                        OrderShippedDate Datetime
                                                        )

--declare date variables 
DECLARE @ToDate as datetime,@FromDate as datetime

--set variales values
 set @ToDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-5, cast(getdate() as datetime)), 110) + ' 23:59:59' AS DATETIME))
 set @FromDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-5, cast(getdate() as datetime)), 110) + ' 13:59:59' AS DATETIME))
 --Insert values into table
Insert Into @FirstTable values(1,  @FromDate, @ToDate)

--set variales values
 set @ToDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-1, cast(getdate() as datetime)), 110) + ' 23:59:59' AS DATETIME))
 set @FromDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-2, cast(getdate() as datetime)), 110) + ' 13:59:59' AS DATETIME))
  --Insert values into table
Insert Into @FirstTable values(2,  @FromDate, @ToDate)

--Print Table Detils
select * from @FirstTable

--FInd Average time
select ID ,AVG(DATEDIFF(day, OrderDate, OrderShippedDate)) as avg_day,
           AVG(DATEDIFF(minute, OrderDate, OrderShippedDate)) as avg_minute,
                   AVG(DATEDIFF(SECOND, OrderDate, OrderShippedDate)) as avg_sec
from @FirstTable
Group by ID

Output:

Hi,

It looks like you have fetched the data from 2 different tables. Accordigly. I ahve re-created the same way but we cannot find out the Average using AVG function on the basis of Date, It should be - Day, Hours, Minutes

--Declare Table
DECLARE @CustomerOrder TABLE (
                                                        CustomerOrderID INT, 
                                                        OrderDate datetime                                                      
                                                        )

--Declare Table
DECLARE @Shipment TABLE (
                                                        CustomerOrderID INT,                                                    
                                                        ShipmentDate Datetime
                                                        )

--declare date variables 
DECLARE @OrderDate as datetime,@OrderShippedDate as datetime

--set variales values
 set @OrderDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-10, cast(getdate() as datetime)), 110) + ' 23:59:59' AS DATETIME))
 set @OrderShippedDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-5, cast(getdate() as datetime)), 110) + ' 13:59:59' AS DATETIME))
 --Insert values into table
Insert Into @CustomerOrder values(1,  @OrderDate)
Insert Into @Shipment values(1,  @OrderShippedDate)
--set variales values
 set @OrderDate = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-3, cast(getdate() as datetime)), 110) + ' 23:59:59' AS DATETIME))
 set @OrderShippedDate  = (SELECT CAST(CONVERT(VARCHAR(10), dateadd(day,-2, cast(getdate() as datetime)), 110) + ' 13:59:59' AS DATETIME))
  --Insert values into table
Insert Into @CustomerOrder values(2,  @OrderDate)
Insert Into @Shipment values(2,  @OrderShippedDate)
--Print Table Detils
select * from @CustomerOrder
select * from @Shipment
--FInd Average time

select CO.CustomerOrderID,avg(datediff(day, CO.OrderDate, S.ShipmentDate))  'AvgTime_day' ,
avg(datediff(HOUR, CO.OrderDate, S.ShipmentDate))  'AvgTime_Hours' 
                from @CustomerOrder CO, @Shipment S 
where CO.CustomerOrderID = S.CustomerOrderID
Group By CO.CustomerOrderID 

Output of that Querry is :


Related Solutions

Based on a study, the average elapsed time between when a user navigates to a website...
Based on a study, the average elapsed time between when a user navigates to a website on a mobile device until its main content is available was 14.6 seconds. This is more than a 20% increase from the previous year. Responsiveness is certainly an important feature of any website and is perhaps even more important on a mobile device. What other web design factors need to be considered for a mobile device to make it more user friendly? Among other...
mobile App Website Design. Based on a 2018 study, the average elapsed time between when a...
mobile App Website Design. Based on a 2018 study, the average elapsed time between when a user navigates to a website on a mobile device until its main content is available was 14.6 seconds. This is more than a 20% increase from 2017. Responsiveness is certainly an important feature of any website and is perhaps even more import-ant on a mobile device. What other web design factors need to be considered for a mobile device to make it more user...
The _____________ lag is the time between when a shock hits the economy and when a...
The _____________ lag is the time between when a shock hits the economy and when a policy responds to it. It is particularly long for _____________ policy. a. outside; monetary b. outside; fiscal c. inside; monetary d. inside;fiscal Ricardian equivalence is the view that a. deficits are intergenerational redistribution from future generations. b. consumers are forward looking, and therefore recognize that a deficit increases their expected future tax burden. Tax cuts that lead to deficits therefore do not induce more...
Q1. In order to estimate the average time spent on the computer terminals per student at...
Q1. In order to estimate the average time spent on the computer terminals per student at a local university, data were collected from a sample of 81 business students over a one-week period. Assume the population standard deviation is 1.2 hours. If the sample mean is 9 hours, then the 95% confidence interval is approximately a. 7.04 to 10.96 hours b. 7.36 to 10.64 hours c. 8.74 to 9.26 hours d. 7.80 to 10.20 hours Q2. In a random sample...
Exhibit F In order to estimate the average time that students spend on the internet per...
Exhibit F In order to estimate the average time that students spend on the internet per day, data were collected for a sample of 36 business students. Assume the population standard deviation is 2 hours. Refer to Exhibit F. If the sample mean is 6 hours, then the 95% confidence interval for the population mean,μ, is Group of answer choices 4.35 to 7.65 5.67 to 6.33 5.35 to 6.65 6 to 6.65
What is EOQ? What would be the average time between orders
What is EOQ? What would be the average time between orders
Explain the difference between an ORDER BY clause and a GROUP BY clause. Provide examples of...
Explain the difference between an ORDER BY clause and a GROUP BY clause. Provide examples of each clause.
Calculate the effective cost of the following trade credit terms when payment is made on the...
Calculate the effective cost of the following trade credit terms when payment is made on the net due date. Use approximate​ cost-of-credit formula. Note​: Assume a​ 30-day month and​ 360-day year. a. 4​/5​, net 45 b. 4​/10​, net 30 c. 3​/10​, net 60 d. 4​/10​, net 60
2a) Calculate an average and sample standard deviation for the independent variable (time of heating) and...
2a) Calculate an average and sample standard deviation for the independent variable (time of heating) and the dependent variable (number of surviving organisms) given the data in the table below. Time (min) ... Number of Survivors 0.1 ...    2.01x10^6 7.5 ... 2.95x10^5 15 ...    8.42x10^4 22.5 ... 2.43x10^4 30 ... 6.99x10^3 2 b) Plot the number of survivors over tie on Cartesian, semi-log and log-log axes using only the data points. Identify the plot that best presents the...
In order to calculate an entity weighted average cost of capital (WACC) financial weight have to...
In order to calculate an entity weighted average cost of capital (WACC) financial weight have to be set each component in the firms capital structure. the weight may be calculated as book or market value and as historic or target figures. Explain what is meant by these four types of weights.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT