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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:54:05+00:00 2026-05-14T18:54:05+00:00

I opened a ticket for this problem. In a nutshell here is my model:

  • 0

I opened a ticket for this problem.

In a nutshell here is my model:

class Plan(models.Model):
 cap = models.IntegerField()

class Phone(models.Model):
 plan = models.ForeignKey(Plan, related_name='phones')

class Call(models.Model):
 phone = models.ForeignKey(Phone, related_name='calls')
 cost = models.IntegerField()

I want to run a query like this one:

Phone.objects.annotate(total_cost=Sum('calls__cost')).filter(total_cost__gte=0.5*F('plan__cap'))

Unfortunately Django generates bad SQL:

SELECT "app_phone"."id", "app_phone"."plan_id",
SUM("app_call"."cost") AS "total_cost"
FROM "app_phone"
INNER JOIN "app_plan" ON ("app_phone"."plan_id" = "app_plan"."id")
LEFT OUTER JOIN "app_call" ON ("app_phone"."id" = "app_call"."phone_id")
GROUP BY "app_phone"."id", "app_phone"."plan_id"
HAVING SUM("app_call"."cost") >=  0.5 * "app_plan"."cap"

and errors with:

ProgrammingError: column "app_plan.cap" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...."plan_id" HAVING SUM("app_call"."cost") >=  0.5 * "app_plan"....

Is there any workaround apart from running raw SQL?

  • 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-14T18:54:06+00:00Added an answer on May 14, 2026 at 6:54 pm

    When aggregating, SQL requires any value in a field either be unique within a group, or that the field be wrapped in an aggregation function which ensures that only one value will come out for each group. The problem here is that “app_plan.cap” could have many different values for each combination of “app_phone.id” and “app_phone.plan_id”, so you need to tell the DB how to treat those.

    So, valid SQL for your result is one of two different possibilities, depending on the result you want. First, you could include app_plan.cap in the GROUP BY function, so that any distinct combination of (app_phone.id, app_phone.plan_id, app_plan.cap) will be a different group:

    SELECT "app_phone"."id", "app_phone"."plan_id", "app_plan"."cap",
    SUM("app_call"."cost") AS "total_cost"
    FROM "app_phone"
    INNER JOIN "app_plan" ON ("app_phone"."plan_id" = "app_plan"."id")
    LEFT OUTER JOIN "app_call" ON ("app_phone"."id" = "app_call"."phone_id")
    GROUP BY "app_phone"."id", "app_phone"."plan_id", "app_plan"."cap"
    HAVING SUM("app_call"."cost") >=  0.5 * "app_plan"."cap"
    

    The trick is to get the extra value into the “GROUP BY” call. We can weasel our way into this by abusing “extra”, though this hard-codes the table name for “app_plan” which is unideal — you could do it programmatically with the Plan class instead if you wanted:

    Phone.objects.extra({
        "plan_cap": "app_plan.cap"
    }).annotate(
        total_cost=Sum('calls__cost')
    ).filter(total_cost__gte=0.5*F('plan__cap'))
    

    Alternatively, you could wrap app_plan.cap in an aggregation function, turning it into a unique value. Aggregation functions vary by DB provider, but might include things like AVG, MAX, MIN, etc.

    SELECT "app_phone"."id", "app_phone"."plan_id",
    SUM("app_call"."cost") AS "total_cost",
    AVG("app_plan"."cap") AS "avg_cap",
    FROM "app_phone"
    INNER JOIN "app_plan" ON ("app_phone"."plan_id" = "app_plan"."id")
    LEFT OUTER JOIN "app_call" ON ("app_phone"."id" = "app_call"."phone_id")
    GROUP BY "app_phone"."id", "app_phone"."plan_id"
    HAVING SUM("app_call"."cost") >=  0.5 * AVG("app_plan"."cap")
    

    You could get this result in Django using the following:

    Phone.objects.annotate(
        total_cost=Sum('calls__cost'), 
        avg_cap=Avg('plan__cap')
    ).filter(total_cost__gte=0.5 * F("avg_cap"))
    

    You may want to consider updating the bug report you left with a clearer specification of the result you expect — for example, the valid SQL you’re after.

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

Sidebar

Ask A Question

Stats

  • Questions 403k
  • Answers 404k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your Delete syntax is invalid. You need to do this… May 15, 2026 at 5:20 am
  • Editorial Team
    Editorial Team added an answer Actually, there is no way. sym('0') creates a symbolic constant… May 15, 2026 at 5:20 am
  • Editorial Team
    Editorial Team added an answer Funnily enough, I asked the exact opposite question yesterday. The… May 15, 2026 at 5:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.