Nested SQL statement

I am trying to extract info from two tables

cd_personal_checks
md_cr_pending

I would like to select id, check_no, & check_amount from cd_personal_checks where Status<=2

I would also like to select the total of initial_amount of all those records in md_cr_pending where the md_cr_pending.cd_personal_checks_id =  cd_personal_checks.id AND md_cr_pending.status=2

here is my SQL statement that i came up with

SELECT m.id, m.check_no, m.check_amount
(select sum(initial_amount) from md_cr_pending where status=2 and cd_personal_checks_id=m.id) as total
FROM cd_personal_checks m
WHERE m.status <=2


however, i also get records where the total = 0 (that is, there are no md_cr_pending records to total)

I would like only those records that have corresponding md_cr_Pending records to total

thanks
geocoins-softwareAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

i2mentalCommented:
SELECT m.id, m.check_no, m.check_amount, sum(b.initial_amount)
from md_cr_pending
join cd_personal_checks m on b.cd_personal_checks_id=m.id
where status=2

This will only give you rows that have a row in cd_personal_checks that matches the check ID in the other table.

If that still gives you 0, then the table has values, but they are all 0's. Try this instead

SELECT m.id, m.check_no, m.check_amount, sum(b.initial_amount)
from md_cr_pending
join cd_personal_checks m on b.cd_personal_checks_id=m.id
where status=2
and sum(b.initial_amount) >0
0
tigin44Commented:
try this

SELECT m.id, m.check_no, m.check_amount, sum(initial_amount) as total
FROM cd_personal_checks m inner join md_cr_pending p on m.id = p.cd_personal_checks_id and p.status = m.status and m.status <=2
group by m.id, m.check_no, m.check_amount
0
geocoins-softwareAuthor Commented:
i2mental  - i get error when running your second statement - haven't tried your first yet - but i will now

tigin44:, your statement works, but you are not using the criteria i asked for

 cd_personal_checks.Status<=2
 md_cr_pending.status=2

i will see if i can alter it though

thanks
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

SharathData EngineerCommented:
check this:
select * from (
SELECT m.id, m.check_no, m.check_amount
(select sum(initial_amount) from md_cr_pending where status=2 and cd_personal_checks_id=m.id) as total
FROM cd_personal_checks m
WHERE m.status <=2) A
where A.total <> 0
0
tigin44Commented:
i missed that part... so this will produce what u want

SELECT m.id, m.check_no, m.check_amount, sum(initial_amount) as total
FROM cd_personal_checks m inner join md_cr_pending p on m.id = p.cd_personal_checks_id and p.status = 2 and m.status <=2
group by m.id, m.check_no, m.check_amount
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
geocoins-softwareAuthor Commented:
Yup, that will work - thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.