I want to compute the average processing time and sending time, grouped by request type, in a single query.
A simple query like:
SELECT avg(processedTime - receivedTime) AS procTime, avg (sentTime - processedTime) AS sendTime FROM requests, responses WHERE requests.requestID = responses.requestID GROUP by requestType
does not work, because the processing time average ends up weighted by how many responses there are to a given request.
Something like
SELECT avg(processedTime - receivedTime), avg(SELECT avg(res.sentTime - req.processedTime) FROM responses res WHERE res.requestID = req.requestID) FROM requests req GROUP BY requestType
does not work either, because the database complains about req.requestID not being part of an aggregate function.
Any ideas? I have gotten this to work by using two separate queries and manually matching up the results, but I’m curious about whether it could be done in one.
If your database supports COUNT DISTINCT:
SELECT sum(processedTime - receivedTime) AS totalprocTime, sum (sentTime - processedTime) AS totalsendTime, count(distinct requests.requestID) reqCount FROM requests, responses WHERE requests.requestID = responses.requestID GROUP by requestType
Then work out averages as total / reqCount for each.
But the problem is, I want to know the average time that it took to send each individual response, not the average time it took to send all the responses.
With that query, the sending time average would be same for 1 response that took 10 seconds, and 5 responses that took 2 seconds each.
You said you had two queries written that worked. Have you tried joining them?
Something like:
Select a.requestType, a.procTime, b.sendTime
from
(QUERY THAT RETURNS procTime) A
INNER JOIN (QUERY THAT RETURNS sendTime ) B
on a.requestType = b.requestType
where WHATEVER CRITERIA
you might need a different type of join.
this assumes that you what works in SQL Server 2005 will work in whatever you are using. And that each of the subqueries returns a two field data set. You might need to tweak the syntax a bit
Processing time is only derived from request data, and should NOT be weighted differently for multiple responses. But send time should weight each response equally.
That’s a humdinger. Let me think about it.
Sample data set that I had to type out to understand, including it for any future benefit to the rest of the thread:
I’m not sure that this would count as a single query, and I don’t have a SQL server handy to test this on at the moment, but would something like this work?
SELECT requestType, procTime, sendTime
FROM
(SELECT requestType, avg(processedTime - receivedTime) AS procTime
FROM requests
GROUP BY requestType) ProcData
INNER JOIN
(SELECT requestType, avg (sentTime - processedTime) AS sendTime
FROM responses
INNER JOIN requests ON responses.requestID = requests.requestID
GROUP BY requestType) SentData
ON ProcData.requestType = SentData.requestType
This is essentially what i said above, except you actually wrote out the 2 subqueries. Assuming each subquery returns the correct results, your query should work. The only thing is that you need to alias requestType after select, or you will get an error (becuase it shows up in both subqueries, it won’t know which one to get). Just replace “requestType” with “ProcData.requestType”.
I thik the OP just wanted one thing he could execute and get all his results. This certanly fits the bill, in my opinion.