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

  • Home
  • SEARCH
  • 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 7733887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:06:40+00:00 2026-06-01T07:06:40+00:00

Hi I have a query which is very straightforward to write in mysql but

  • 0

Hi I have a query which is very straightforward to write in mysql but I’m having difficulty translating to Django:

SELECT year, month, AVG(mean_air_temp), AVG(min_air_temp), AVG(max_air_temp) from climate WHERE recorded_on >= '1978-09-09' AND recorded_on <= '1988-09-09' GROUP by month;

My data looks like this:

+----+------+-------+-------------+---------------+--------------+--------------+----------+--------------+---------------+
| id | year | month | recorded_on | mean_air_temp | min_air_temp | max_air_temp | sea_temp | mean_rel_hum | precipitation |
+----+------+-------+-------------+---------------+--------------+--------------+----------+--------------+---------------+
|  1 | 1964 |    12 | 1964-12-31  |          26.1 |         22.2 |         30.8 |     25.8 |           82 |             0 |
|  2 | 1965 |     1 | 1965-01-01  |          23.9 |           21 |         25.8 |     22.7 |           84 |           0.7 |
|  3 | 1965 |     1 | 1965-01-02  |          23.4 |         21.1 |         26.2 |     22.4 |           83 |             0 |

I am basically trying to get averages by month, and with an another query, by year in the most efficient way possible. The fields to be averaged will be python variables in the view, as will be the dates and group by method.

Is there a native Django solution or do I need to do something with .extra() (although I would like to avoid this so that I’m not tied to mysql)? I have looked at the docs for aggregate but they aren’t helping in this situation.

Any help would be much, much appreciated.

  • 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-01T07:06:42+00:00Added an answer on June 1, 2026 at 7:06 am

    Have you tried:

    from django.db.models import Count
    from django.db.models import Avg, Max, Min, Count
    
    
    un_test_able = Climate.objects.filter(
                               recorded_on__gte='1978-09-09',
                               recorded_on__lte='1988-09-09')
                          .values('month')
                          .annotate(mean_air_temp=AVG('mean_air_temp'), 
                                    min_air_temp=AVG('min_air_temp'), 
                                    max_air_temp=AVG('max_air_temp')
                          )
    

    Dynamic Version, not sure how dynamic you need but you can build a keyword arg dictionary however you like and pass it to the annotate function.

    Example of 1 dynamic key…

    key_mean = 'mean_air_temp'
    query_value_mean='mean_air_temp'
    kwargs = {
                key_mean:AVG(query_value_mean), 
                'min_air_temp':AVG('min_air_temp'), 
                'max_air_temp':AVG('max_air_temp')
    }
    un_test_able = Climate.objects.filter(
                               recorded_on__gte='1978-09-09',
                               recorded_on__lte='1988-09-09')
                          .values('month')
                          .annotate(**kwargs)
                          )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following query which has select query that returns data in 5sec. But
I have very long select query which i need to filter based on some
I have a query which takes a very long time to run but produces
Well friends, I have got this query which works but is very long for
I have a very expensive query which gets executed from php and it can
I have a very basic query string which passes a ID to a receiving
I have this query which works correctly in MySQL. More background on it here
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I have 3 different transaction tables, which look very similar, but have slight differences.
Hello Friends i have the following query which defeat the very purpose. A day

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.