Nested SQL statement

I am trying to extract info from two tables


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 = AND md_cr_pending.status=2

here is my SQL statement that i came up with

SELECT, m.check_no, m.check_amount
(select sum(initial_amount) from md_cr_pending where status=2 and 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

Who is Participating?

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

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.


SELECT, m.check_no, m.check_amount, sum(p.initial_amount) as total 
FROM cd_personal_checks m Inner Join md_cr_pending p 
On ( 
WHERE m.status <=2
And p.status=2 
Group By, m.check_no, m.check_amount 
Having sum(p.initial_amount) > 0

Open in new window


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:
It worked, but, Sorry, that returned no records.

I have solved it though with this - from another expert

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

For future reference, this question is a duplicate of

Rather than one question being posted in several Topic Areas, separate questions were posted in multiple TAs.  The "from another expert" refers to a solution posted in the other thread.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

geocoins-softwareAuthor Commented:
yes, that was my bad....but regardless.....your solution still didn't work...

"...your solution still didn't work"

I'm not asking for credit/points for solving your problem.  Both my suggested SQL and the accepted answer in the other thread are almost identical, using a Group By instead of a (Select ) column.  The results difference seems to be the table-linking criteria.

In the future, when you post your questions, please keep in mind that we don't have a copy of your database with which to validate and test our SQL prior to suggesting solutions.  There is some expectation that you will provide us enough information to solve your problem and that you are responsible for both testing the solutions and providing feedback to the experts.
geocoins-softwareAuthor Commented:
>responsible for both testing the solutions and providing feedback to the experts

and I believe that is exactly what i did....unless of course you didn't see my comment

>It worked, but, Sorry, that returned no records

which of course assumes that i tested it - if of course i am stating it didn't return any records

Thanks for the lecture though.

I consider this closed...please do the same


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

From novice to tech pro — start learning today.