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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:03:11+00:00 2026-05-20T01:03:11+00:00

So I have this query here and I can’t figure out how to change

  • 0

So I have this query here and I can’t figure out how to change the behavior. Basically it’s supposed to return a set of results from the applications table, with the total payments from the payments table with a corresponding id, and with the total fees from the fees table with the corresponding id. It works perfect when there’s one payment for one fee. However, in cases where there are 2 fees, the payments seem to be doubling. I’d assume if there were 3 fees, it would triple, etc.

I tried doing sum(distinct payments.amount) as payments, but the problem is it’s entirely likely that there’d be 2 payments for the exact same amount. I’m not sure if it’s possible to like, do a sum where there’s a distinct id corresponding to the amount.. I’m still learning SQL.

select applications.*,
permits.title as permit,
sum(payments.amount) as payments,
sum(fees.unitprice) as fees
from applications
left join permits on applications.permitid=permits.id
left join payments on payments.appid=applications.id
left join fees on fees.appid=applications.id
where applications.deleted=0
and payments.deleted=0
and fees.deleted=0
and fees.unitPrice > 0
and applicationdate between '1975-1-1' and '2011-2-10'
group by applications.id
order by permits.deptid,permits.title,status,dateissued,applicationdate

Any help would be appreciated – I’ve been trying to fix this for hours! Took me a few just to figure out what was causing it.

  • 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-20T01:03:11+00:00Added an answer on May 20, 2026 at 1:03 am

    You can do the aggregation in a derived table and join onto that.

    ... join (SELECT fees.appid, sum(fees.unitprice) as fees
                      FROM   fees
                      WHERE  fees.deleted = 0
                             and fees.unitPrice > 0
                      GROUP  BY fees.appid) aggregatedfees
             on aggregatedfees.appid = applications.id 
     ...
    

    BTW: All of your left joins are being converted to inner joins at the moment by your where clause.

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

Sidebar

Related Questions

I have to migrate this query (simplified here) from T-SQL to ORACLE SET IDENTITY_INSERT
Pulling my hair out with this query. Maybe some of the experts here can
I have this query which works correctly in MySQL. More background on it here
I have this query: UPDATE `terms` SET id = '15', taxonomy_id = '1', parent_id
I have this query that returns the results by ordering it by focus.name ASC.
Found a couple of similar questions here on this, but couldn't figure out how
Found a couple of similar questions here on this, but couldn't figure out how
I am trying to get a query work! I have this query right here:
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this query... SELECT Distinct([TargetAttributeID]) FROM (SELECT distinct att1.intAttributeID as [TargetAttributeID] FROM AST_tblAttributes

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.