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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:48:05+00:00 2026-06-09T22:48:05+00:00

when i join two tables together and get the sum, i am getting the

  • 0

when i join two tables together and get the sum, i am getting the correct figure:

select 
    d.accn_id,
    cast(d.load_date as DATE) as LoadDate,
    cast (d.final_rpt_date as DATE) as FinalReportDate,
    sum(p.paid_amt) as SumPaidAmt

from
    accn_demographics d
join 
    accn_payments p
on 
    d.ACCN_ID=p.ACCN_ID
where 
    p.POSTED='y'
    and p.PMT_DATE between '20120501' and '20120531'

group by 
    d.accn_id,
    d.load_date,
    d.final_rpt_date

however after i join ANOTHER table accn_payors:

select 
    d.accn_id,
    cast(d.load_date as DATE) as LoadDate,
    cast (d.final_rpt_date as DATE) as FinalReportDate,
    sum(p.paid_amt) as SumPaidAmt
    ,payors.PAYOR_ID
from
    accn_demographics d
join 
    accn_payments p
on 
    d.ACCN_ID=p.ACCN_ID
left join 
    accn_payors payors
on  
    payors.X_PAYOR_ID=p.X_PRICED_PAYOR_ID 
    and payors.ACCN_ID = p.ACCN_ID
where 
    p.POSTED='y'
    and p.PMT_DATE between '20120501' and '20120531'

group by 
    d.accn_id,
    d.load_date,
    d.final_rpt_date
    ,payors.PAYOR_ID

i am getting an overstatement of sum(p.paid_amt)

the question is how can i adjust my join so that i am not joining multiple times?

  • 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-09T22:48:06+00:00Added an answer on June 9, 2026 at 10:48 pm

    Instead of joining on accn_payors, you need to join on a SELECT statement that returns one row per accn_id. Depending on your requirements, this might work.

    select accn_id, min(x_payor_id) x_payor_id
    from payors
    group by accn_id
    

    Aliasing min(x_payor_id) with the same name lets the rest of the code work without modification. That might or might not be a good idea. It’s a little misleading.

    So instead of this . . .

    left join 
        accn_payors payors
    on  
        payors.X_PAYOR_ID=p.X_PRICED_PAYOR_ID 
        and payors.ACCN_ID = p.ACCN_ID
    

    you’d do this . . .

    left join 
        (select accn_id, min(x_payor_id)
         from payors
         group by accn_id) payors
    on  
        payors.x_payor_id = p.x_priced_payor_id
        and payors.accn_id = p.accn_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I want to join together. Table1 Year, ID, Theme,
I have two tables which I want to join together using a left outer
My situation is I have two tables. I want to join them together and
alt text http://img502.imageshack.us/img502/7245/75088152.jpg There are two tables that I join them together, one of
I want to join two tables using JPQL : SELECT * FROM A LEFT
I want to join two tables with several hundred columns, like this: select *
I want to join two tables (below) which i get. But then I want
I have three tables that I need to join together and get a combination
I want to join two tables together and have ONLY the data in Table
I am trying to join two tables together using C# linq to entities. One

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.