Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:59:43+00:00 2026-06-18T10:59:43+00:00

I have a query that I need for it to select the average time

  • 0

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 :

  1. Select the reason (why)
  2. Count how many times a student has come in for that reason count(why)
  3. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:59:44+00:00Added an answer on June 18, 2026 at 10:59 am
    SELECT 
    session.why AS Reason, 
    COUNT(session.why) AS Count, 
    SEC_TO_TIME(AVG(TIMEDIFF(t.fin, session.signintime))) AS Time
    FROM session 
    LEFT JOIN (SELECT support.session_id, MAX(support.finishtime) AS fin 
    FROM support 
    GROUP BY support.session_id) AS t 
        ON session.session_id = t.session_id 
    WHERE session.status = 3  
    GROUP BY session.why 
    ORDER BY session.signintime DESC
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables described below. What I need is a single query that
Need help with a query that I wrote: I have three tables Company id
I have a query that I need to loop. query=select '$dbserver' as server; while
I have a query that I need to execute that I do not know
I have a query such that I need to get A specific dog All
Using Microsoft SQL server 2008 I have a query that is in need of
I have a dynamic query string that I need to pass via an .htaccess
I have a query that does what i want joining table but i need
Say I have a query that fetches [type][show_name]. For all [type]==5 records, I need
I currently have a SQL query that returns a number of fields. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.