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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:14:01+00:00 2026-06-04T19:14:01+00:00

I am trying to get the maximum value from merging the results from two

  • 0

I am trying to get the maximum value from merging the results from two separate tables. I am trying to get the most updated comment for a Post. I have a model Comment that can be accessed by Post.comments. I also have a body that is separate of the Post that can be accessed by Post.body. Both comments and body have a field when that is a DateTimeField. I would like to return a sorted queryset by most recent activity such that the a Post with the most recent comment or body is shown first.

Assume the models look like this:

class Body(models.Model):
    when = models.DateTimeField(default=datetime.datetime.now)

class Post(models.Model):
    body = models.ForeignKey(Body)

class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments')
    when = models.DateTimeField(default=datetime.datetime.now)

I would like the result to remain a queryset if possible since I continue to process it and further pass it into a paginator to limit results.

Basically, I would like to be able to call:

q = Post.annotate(
    activity=Max(
        Union('comments__when', 'body__when')
    )
)
q = q.order_by('-activity')

But I don’t know how to accomplish that Union.

I believe the SQL that accomplishes what I’m looking for is comparable to:

SELECT
...
IF(MAX(c.`when`), MAX(c.`when`), b.`when`) AS `activity`
...
FROM `post` p
...
LEFT OUTER JOIN `comment` AS c
ON c.`post_id`=p.`id`
LEFT OUTER JOIN `body` AS b
ON p.`body_id`=b.`id`
...

Can such a customized annotation and Join be accomplished?

  • 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-04T19:14:03+00:00Added an answer on June 4, 2026 at 7:14 pm

    This took me an extremely long time to figure out. One large issue is that Django does not support multiple conditions for the ON clause of a JOIN statement. Because of the dependence of a single SELECT statement upon two separate JOINs, we have an issue tracking the table names accurately. When you ask django to annotate a table, say with Max(), you end up with conditions such as Max(T7.when) ... LEFT OUT JOIN table AS T7, where T7 is not consistent. Thus, I needed a way to accruately generate the expression Max(T7.when) given the JOINs automatically generated by Django. Many posts online tell you to use the .raw() support on querysets, but you’ll lose a lot of benefit from using an ORM in this case.

    The solution I came up with was to generate a custom aggregate function. I called this function CustomMax():

    from django.conf import settings
    from django.db import models
    
    sum_if_sql_template = 'GREATEST(%(alt_table)s.%(alt_field)s, IFNULL(%(function)s(%(field)s), %(alt_table)s.%(alt_field)s))'
    
    class CustomMaxSQL(models.sql.aggregates.Aggregate):
        sql_function = 'MAX'
        sql_template = sum_if_sql_template
    
    
    class CustomMax(models.aggregates.Aggregate):
        name = 'Max'
    
        def __init__(self, lookup, **extra):
            self.lookup = lookup
            self.extra = extra
    
        def add_to_query(self, query, alias, col, source, is_summary):
            aggregate = CustomMaxSQL(col,
                                 source=source,
                                 is_summary=is_summary,
                                 **self.extra)
            query.aggregates[alias] = aggregate
    

    The usage of the function is:

    q = Post.annotate(
        activity=CustomMax(
            'comments__when',
            alt_table="app_post",
            alt_field="when",
        )
    )
    q.query.join((
        'app_post',
        'app_comment',
        'id',
        'post_id',
    ))
    q = q.order_by('-activity')
    

    I include the .join() to allow for the alt_table to exist as a JOIN, and Django will automatically handle the naming of both the SELECT and JOIN statements for the Max portion. The generated SQL from this usage is akin to:

    SELECT 
    ...
    GREATEST(app_post.when, IFNULL(MAX(T7.`when`), app_post.when)) AS `activity`,
    ...
    `app_post`.`id`
    ...
    INNER JOIN `app_post` ON ...
    ...
    LEFT OUTER JOIN `app_comment` T7 ON (`app_post`.`id` = T7.`post_id`)
    ...
    

    Note: This is just for Post and Comment models from above. My actual implementation was a bit more complicated and required this solution.

    This would also have been a lot easier if the Django team had developed this suggested patch to include join statements in .extra(): https://code.djangoproject.com/ticket/7231

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

Sidebar

Related Questions

I am trying to get all users that are updated maximum 90 seconds ago:
I'm trying to get the maximum value of an integer field in a table.
I'm trying to get the maximum value for an attribute in an Entity in
I'm trying to get the maximum frequency on a histogram graph. I have a
I am using iOS 5.0 and I'm trying to get a value from my
I'm trying to get the maximum value in a line in an 2d array.
I'm trying to extract from a table the value that appears the maximum number
Trying to get text value of child in expandableListView. Getting a nullpointerexception in the
I'm trying to write a query which will get the maximum scores for the
What I am trying to achieve is to get the rows having the maximum

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.