Solved

Pervasive SQL syntax

Posted on 2014-12-30
5
225 Views
Last Modified: 2014-12-30
Imagine I have Table A with one record.  The "Mortgage" field contains 10,000.

Table B (other loans) has two records (because this one guy has a second and third mortgage). The "Amount" fields contain $300 and $500.

When I use a left outer join to add up the Mortgage and Amount fields I get 20,800.

I assume that this is because the left outer join returns 2 records... 10,000 and 300 as well as 10,000 and 500.

SELECT 
  "ID" AS "ID",
  "strName" AS "Borrower Name",
  SUM(("Mortgage" + IF("Amount" IS NOT NULL,"Amount",0))) AS "Total Owed"

FROM 
  "TableA"
LEFT OUTER JOIN
  "TableB"

ON
  "MLoan" = "OLoan"

GROUP BY 
  "ID", "strName"

ORDER BY
  "ID"

Open in new window


How can I get this to return 10,800?
0
Comment
Question by:classnet
  • 2
  • 2
5 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40524424
YOu could use a union query:

SELECT Sum(U.Amount) as AmountOwed
FROM (
SELECT TableA.Mortage as Amount
FROM TableA
UNION ALL
SELECT TableB.Amount
FROM TableB
) as U
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 40524425
You want to sum the total in table A and add it to the summed total in table B.

No join is necessary.
0
 

Author Closing Comment

by:classnet
ID: 40524495
This lead me to the answer... thanks.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40524614
The answer I provided does exactly what Kdo recommends.  Did you try my solution?
0
 

Author Comment

by:classnet
ID: 40524632
Did not... I desired to just eliminate the join.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL: Do I need CLUSTERED here? 13 59
T-SQL Default value in Select? 5 38
Need help constructing a conditional update query 16 71
awk and Pythagoras? 5 19
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question