Get Average from SQL Server Query Results

Hello. I have modified a  SQL Server query that I got from a friend to output the total execution for each process. It outputs the results as 00:00:00 and the datatype is varchar. I want to combine all the results and get an average. However, I cannot figure out how to convert it from varchar to decimal so I can get the average. Any assistance is appreciated.

Current Results
Total
00:52:34
01:31:33
0:15:27

Desired Results
Total
00:53:11

Query
;WITH CTETotal AS (SELECT CASE  
	WHEN DATEDIFF(dd, tsstart.Time, tslast.Time) > 0 THEN 
	CAST(DATEDIFF(dd, tsstart.Time, tslast.Time) AS varchar(2)) + 'd ' 
	ELSE '' 
	END + CONVERT(varchar(8), tslast.Time - tsstart.Time, 108) Total
FROM 
( 
	SELECT 
	MAX(ExecutionTime) AS Time, 
	AdvertisementID, 
	ResourceID 
	FROM v_TaskExecutionStatus 
	WHERE LastStatusMessageID = 11140 AND Step = 0 and
	ExecutionTime  > DATEADD(month, -1,GETDATE())
	GROUP BY AdvertisementID, ResourceID 
) tsstart  
INNER JOIN 
( 
	SELECT 
	AdvertisementID, 
	ResourceID, 
	LastStatusTime as Time 
	FROM v_ClientAdvertisementStatus
	WHERE LastState = 13 AND
	AdvertisementID = 'MXXXXX'
) tslast ON tsstart.AdvertisementID = tslast.AdvertisementID AND 
tsstart.ResourceID = tslast.ResourceID AND 
tslast.Time > tsstart.Time)
SELECT Total
FROM CTETotal

Open in new window

LorrecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
No sample data, but maybe?

;WITH CTETotal AS (SELECT
      CASE WHEN AvgSecs >= 86400 THEN CAST(AvgSecs / 86400 AS varchar(20)) + 'd '
    ELSE ''
    END + CONVERT(varchar(8), DATEADD(SECOND, AvgSecs % 86400, 0)) Total
FROM (
    SELECT  
    CEILING(SUM(DATEDIFF(SECOND, tsstart.Time, tslast.Time)) * 1.0 / COUNT(*)) AS AvgSecs
FROM (
(
      SELECT
      MAX(ExecutionTime) AS Time,
      AdvertisementID,
      ResourceID
      FROM v_TaskExecutionStatus
      WHERE LastStatusMessageID = 11140 AND Step = 0 and
      ExecutionTime  > DATEADD(month, -1,GETDATE())
      GROUP BY AdvertisementID, ResourceID
) tsstart  
INNER JOIN
(
      SELECT
      AdvertisementID,
      ResourceID,
      LastStatusTime as Time
      FROM v_ClientAdvertisementStatus
      WHERE LastState = 13 AND
      AdvertisementID = 'MXXXXX'
) tslast ON tsstart.AdvertisementID = tslast.AdvertisementID AND
tsstart.ResourceID = tslast.ResourceID AND
tslast.Time > tsstart.Time)) AS final)
SELECT Total
FROM CTETotal
0
LorrecAuthor Commented:
Thank you for the response.  I receive the below output.

Total
Jan  1 1
0
Scott PletcherSenior DBACommented:
OOPS, I used the wrong format code in the output -- 0 instead of 108.  Here's corrected:

;WITH CTETotal AS (SELECT
      CASE WHEN AvgSecs >= 86400 THEN CAST(AvgSecs / 86400 AS varchar(20)) + 'd '
    ELSE ''
    END + CONVERT(varchar(8), DATEADD(SECOND, AvgSecs % 86400, 108)) Total
FROM (
    SELECT  
    CEILING(SUM(DATEDIFF(SECOND, tsstart.Time, tslast.Time)) * 1.0 / COUNT(*)) AS AvgSecs
FROM (
(
      SELECT
      MAX(ExecutionTime) AS Time,
      AdvertisementID,
      ResourceID
      FROM v_TaskExecutionStatus
      WHERE LastStatusMessageID = 11140 AND Step = 0 and
      ExecutionTime  > DATEADD(month, -1,GETDATE())
      GROUP BY AdvertisementID, ResourceID
) tsstart  
INNER JOIN
(
      SELECT
      AdvertisementID,
      ResourceID,
      LastStatusTime as Time
      FROM v_ClientAdvertisementStatus
      WHERE LastState = 13 AND
      AdvertisementID = 'MXXXXX'
) tslast ON tsstart.AdvertisementID = tslast.AdvertisementID AND
tsstart.ResourceID = tslast.ResourceID AND
tslast.Time > tsstart.Time)) AS final)
SELECT Total
FROM CTETotal
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

awking00Information Technology SpecialistCommented:
;WITH CTETotal AS (SELECT CASE  
	WHEN DATEDIFF(dd, tsstart.Time, tslast.Time) > 0 THEN 
	CAST(DATEDIFF(dd, tsstart.Time, tslast.Time) AS varchar(2)) + 'd ' 
	ELSE '' 
	END + CONVERT(varchar(8), tslast.Time - tsstart.Time, 108) Total
FROM 
( 
	SELECT 
	MAX(ExecutionTime) AS Time, 
	AdvertisementID, 
	ResourceID 
	FROM v_TaskExecutionStatus 
	WHERE LastStatusMessageID = 11140 AND Step = 0 and
	ExecutionTime  > DATEADD(month, -1,GETDATE())
	GROUP BY AdvertisementID, ResourceID 
) tsstart  
INNER JOIN 
( 
	SELECT 
	AdvertisementID, 
	ResourceID, 
	LastStatusTime as Time 
	FROM v_ClientAdvertisementStatus
	WHERE LastState = 13 AND
	AdvertisementID = 'MXXXXX'
) tslast ON tsstart.AdvertisementID = tslast.AdvertisementID AND 
tsstart.ResourceID = tslast.ResourceID AND 
tslast.Time > tsstart.Time),
CTE2 as
(select cast(left(total,2) as int) * 3600 +
 cast(substring(4,2) as int) * 60 +
 cast(right(total,2) as int as totsecs
 FROM CTETotal),
CTE3 as
(select 
right('00' + cast(floor(avgsecs/3600) as varchar(2)),2) + ':' +
right('00' + cast(floor(avgsecs/60) - floor(avgsecs/3600) as varchar(2)),2)
right('00' + cast(avgsecs - (floor(avgsecs/60) - floor(avgsecs/3600)) * 60 as varchar(2)),2) as avgtot
from cte3;

Open in new window

0
LorrecAuthor Commented:
Thank you for the assistance. Much appreciated.
0
awking00Information Technology SpecialistCommented:
This line needs a parenthesis -
cast(right(total,2) as int) as totsecs
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.