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 8677989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:36:05+00:00 2026-06-12T20:36:05+00:00

I am trying to get the average number of months difference between two dates:

  • 0

I am trying to get the average number of months difference between two dates:

select client_id, 
        avg(12*(year(MAX(received_date))-year(min(received_date))) 
        + MONTH(MAX(received_date))-MONTH(min(received_date)))
        from tmpTwoAccessions
        group by CLIENT_ID,PATIENT_ID

I am getting this message:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Can you please advise me on what am I doing incorrectly here?
Thanks so much for your guidance.

  • 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-12T20:36:06+00:00Added an answer on June 12, 2026 at 8:36 pm

    You do the difference between the MAX and MIN in a sub-query, and then average that in the outer query.

    SELECT client_id, AVG(delta_date)
      FROM (SELECT client_id, patient_id,
                   12 * (YEAR(MAX(received_date))-YEAR(MIN(received_date))) 
                   + MONTH(MAX(received_date)) - MONTH(MIN(received_date))) AS delta_date
              FROM tmpTwoAccessions
             GROUP BY client_id, patient_id
           ) AS x
     GROUP BY client_id;
    

    I’m not convinced your date difference expression is optimal, but I’ve not tried changing it.


    Yurly Rozhovetskiy suggested the expression:

    DATEDIFF(MONTH, MIN(received_date), MAX(received_date))
    

    which looks good to me. The revised query becomes:

    SELECT client_id, AVG(delta_date)
      FROM (SELECT client_id, patient_id,
                   DATEDIFF(MONTH, MIN(received_date), MAX(received_date)) AS delta_date
              FROM tmpTwoAccessions
             GROUP BY client_id, patient_id
           ) AS x
     GROUP BY client_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the average number of days between when a user closes
I have two tables points and contacts and I'm trying to get the average
I am trying to get the rounded number of the average load in the
I'm trying to get the average number of interactions on my metrics, but this
I'm trying to get an average of sums using nested aggregate functions and grouping.
I am trying to get the average of the rating (found in the review
I'm trying to get the average of a ratio in SQL. My table looks
I'm trying to get the MODE average for a set of zipcodes (zip code
I'm trying to get an average of a distinct count per day out of
In my current table i am trying to get average of columns based on

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.