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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:22:41+00:00 2026-06-17T16:22:41+00:00

(This is a rewrite of my previous question which may not have been clear

  • 0

(This is a rewrite of my previous question which may not have been clear enough)

I have a query for a MYSQL database which is as follows:

SELECT name,
SUM(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0)) AS `month1`, 
SUM(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0)) AS `month2`, 
SUM(IF(date_format (date, '%b, %Y')= 'Feb, 2012', 1,0)) AS `month3`, 
etc...

Which gets me a series of results like – month1=55, month2=70, month3=89 etc

In the query is a line –

COUNT(*) AS total FROM table order by total

Which effectively gives me a total of month1+month2+month3+ etc

However I also need to get an average of those same monthly totals

So I need a MySQL function that would effectively be something like

AVG (month1, month2, month3 etc) 

which would give the average of 55,70,89

Can anyone help?

Thanks very much

AS REQUESTED, COMPLETE QUERY IS –

SELECT name, 
    SUM(IF(date_format (date, '%b, %Y')= 'Nov, 2011', 1,0))/list*1000 AS `month1`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0))/list*1000 AS `month2`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0))/list*1000 AS `month3`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Feb, 2012', 1,0))/list*1000 AS `month4`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Mar, 2012', 1,0))/list*1000 AS `month5`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Apr, 2012', 1,0))/list*1000 AS `month6`, 
    SUM(IF(date_format (date, '%b, %Y')= 'May, 2012', 1,0))/list*1000 AS `month7`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Jun, 2012', 1,0))/list*1000 AS `month8`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Jul, 2012', 1,0))/list*1000 AS `month9`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Aug, 2012', 1,0))/list*1000 AS `month10`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Sep, 2012', 1,0))/list*1000 AS `month11`, 
    SUM(IF(date_format (date, '%b, %Y')= 'Oct, 2012', 1,0))/list*1000 AS `month12`, 
    COUNT(*) AS total 
FROM table 
group by name 
order by total 
  • 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-17T16:22:42+00:00Added an answer on June 17, 2026 at 4:22 pm

    In your case you can use a subquery –

    SELECT name,
      `month1`, `month2`, `month3`
      total,
      (`month1` + `month2` + `month3`) / 3 AS `avg`
    FROM
      (SELECT name, 
        SUM(IF(date_format (date, '%b, %Y')= 'Nov, 2011', 1,0))/list*1000 AS `month1`, 
        SUM(IF(date_format (date, '%b, %Y')= 'Dec, 2011', 1,0))/list*1000 AS `month2`, 
        SUM(IF(date_format (date, '%b, %Y')= 'Jan, 2012', 1,0))/list*1000 AS `month3`, 
        COUNT(*) AS total 
      FROM table 
      GROUP BY name 
      ORDER BY total
      ) t
    

    But I’d suggest you to use something like this –

    SELECT month, AVG(cnt) cnt FROM
      (SELECT MONTH(DATE) month, COUNT(*) cnt FROM table1 GROUP BY month) t
    GROUP BY month WITH ROLLUP
    

    …you only should add year support.

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

Sidebar

Related Questions

NOTE: This is an entire rewrite of the previous question. I have a model.
I need to rewrite this query and I'm not allowed to use a subquery.
Sorry I changed the previous question. I have problem with .htaccess rewrite rule on
I asked this question earlier: mod_rewrite: match only if no previous rules have matched?
Note: This is a sequel to my previous question about powersets. I have got
How can I rewrite this query to Doctrine DBAL query or better to createQuery()
How would I rewrite this query to be performant by executing the SQL function
In a previous question I was told how to rewrite my computation expressions so
We have a number of machines which record data into a database at sporadic
I've decided to rewrite a database I have that is poorly normalized. I've created

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.