Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on 

Pervasive SQL syntax

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?
SQL

Avatar of undefined
Last Comment
Sheldon Livingston
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sheldon Livingston

ASKER

This lead me to the answer... thanks.
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

The answer I provided does exactly what Kdo recommends.  Did you try my solution?
Avatar of Sheldon Livingston

ASKER

Did not... I desired to just eliminate the join.
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo