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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:48:13+00:00 2026-05-18T00:48:13+00:00

Using MySQL. How can I find the average of the min_amount and max_amount columns

  • 0

Using MySQL. How can I find the average of the min_amount and max_amount columns while grouping by year(my_date) and month(my_date)?

Currently I’m planning on putting the sum of the columns in an array then average them using PHP.

Is there a way to get the average of the 2 grouped columns with an SQL query instead?

$sql = "SELECT
    SUM(min_amount) AS sum_min_amount,
    SUM(max_amount) AS sum_max_amount,
    my_date
FROM
    table_1
WHERE
    name = '$name'
AND
    year(my_date) BETWEEN '$start_year' AND '$end_year'
AND
    month(my_date) BETWEEN '$start_month' AND '$end_month'
GROUP BY
   year(my_date), month(my_date)";
  • 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-05-18T00:48:14+00:00Added an answer on May 18, 2026 at 12:48 am

    MySQL has an average function built in, so there should be no need to do this calculation on the PHP side of things, unless you have some hideous business requirements that can’t be easily expressed in a query.

    SELECT YEAR(my_date) AS year,
        MONTH(my_date) AS month,
        AVERAGE(min_amount) AS min_avg,
        AVERAGE(max_amount) AS max_avg
    FROM table_1
    WHERE (my_date >= "$start_year-$start_month-01")
        AND (my_date <= "$end_year-$end_month-31")
        AND (name = $name)
    GROUP BY year, month
    

    You’d probably want to properly build the data strings outside of the query, and most definitely use a prepared statement for this, as the usual “you’re wide open to SQL injection” attacks holds here.

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

Sidebar

Related Questions

i can't display the image on my aspx view.. i'm using mysql as database
I'm using a MySQL database. I have a products database where users can search
Using PHP, I can convert MySQL data or static table data to csv, Excel,
How can I make a perfect backup of mysql database using mysqldump? When I
I was wondering how can I validate a single checkbox using PHP and MySQL.
I would like to access a MySQL database using PHP. Can someone please explain
How can I insert an image in MySQL and then retrieve it using PHP?
I am using the following InnoDB tables in mysql to describe records that can
can i perform cascaded delete using foreign key constraints in MySql 3.0 ?
i save in mysql database the time using NOW() function. how can i change

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.