I have a query that I need for it to select the average time difference of two different times from two separate tables.
That seemed easy until the next part of the query came in : I have to group by the reason for a student coming into the office. So now this query needs to :
- Select the reason (why)
- Count how many times a student has come in for that reason count(why)
- Then I need to check the AVG time from the starttime to finishtime. Keep in mind that I am grouping by why. This means that I need the timediff to compute difference for all records that fall within the reason for the students visit.
I wrote this query :
SELECT why,
count(why),
SEC_TO_TIME(AVG(MAX(support.finishtime) - session.signintime))
FROM session
LEFT JOIN support
ON support.session_id = session.session_id
WHERE status = 3
GROUP BY why;
However I get a error :
ERROR 1111 (HY000): Invalid use of group function
I don’t seem to understand this problem. From reading past questions they are considering to use having? But I don’t understand how or even where in this situation where to add the having clause.
Any help would be much appreciated.
Thank you
This does the job perfectly! I just got it a couple hours ago, I never really worked with sub queries so my professor helped me out with it.