Solved

MySQL select by subquery

Posted on 2014-02-05
3
606 Views
Last Modified: 2014-02-05
Hi,

I have a query which selects an invoices oustanding balance. I am doing this using subqueries and can sort by the result 'queryBalance' but I cannot select by this 'queryBalance' in the event of wanting to return only results where the balance is 0 or > 0



 select
       Invoice.invNo,
    Customer.FirstName,
    Customer.Surname,
    Customer.Town,
    Customer.pCode,
    customer.automaticPayments,
    invoice.Gross,
    (SELECT ifnull(SUM(totPaid),0) FROM cashRcd WHERE cashRcd.invNo = invoice.invNo) AS totPaid,
    (SELECT ifNull(SUM(Gross),0) FROM creditNote WHERE creditNote.invNo = invoice.invNo) AS totCredited,
    (SELECT invoice.Gross - totCredited - totPaid ) AS queryBalance
    FROM Invoice
    INNER JOIN invoiceStatus
    ON invoice.status = invoiceStatus.statusID
    INNER JOIN Customer
    ON Invoice.CustomerId = Customer.cId
    WHERE invoice.Deleted = 0
    ORDER BY queryBalance DESC

Any ideas appreciated.

Regards
Leigh
0
Comment
Question by:Lmillard
3 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39835496
you can put the full query into subquery:
 select *
  from ( select
       Invoice.invNo,
    Customer.FirstName,
    Customer.Surname,
    Customer.Town,
    Customer.pCode,
    customer.automaticPayments,
    invoice.Gross,
    (SELECT ifnull(SUM(totPaid),0) FROM cashRcd WHERE cashRcd.invNo = invoice.invNo) AS totPaid,
    (SELECT ifNull(SUM(Gross),0) FROM creditNote WHERE creditNote.invNo = invoice.invNo) AS totCredited,
    (SELECT invoice.Gross - totCredited - totPaid ) AS queryBalance
    FROM Invoice
    INNER JOIN invoiceStatus
    ON invoice.status = invoiceStatus.statusID
    INNER JOIN Customer
    ON Invoice.CustomerId = Customer.cId
    WHERE invoice.Deleted = 0
 ) sq
where sq.queryBalance > 0
    ORDER BY queryBalance DESC 

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 39835748
You also can use derived tables instead, so that you can perform calculation on queryBalance in the WHERE clause.
SELECT Invoice.invNo
     , Customer.FirstName
     , Customer.Surname
     , Customer.Town
     , Customer.pCode
     , Customer.automaticPayments
     , Invoice.Gross
     , IFNULL(rcpts.totPaid, 0) AS totPaid
     , IFNULL(crdts.totCredited, 0) AS totCredited
     , Invoice.Gross - IFNULL(crdts.totCredited, 0) - IFNULL(rcpts.totPaid, 0) AS queryBalance
FROM Invoice 
INNER JOIN invoiceStatus ON invoice.status = invoiceStatus.statusID
INNER JOIN Customer ON Invoice.CustomerId = Customer.cId
LEFT JOIN (
    SELECT invNo, SUM(totPaid) AS totPaid
    FROM cashRcd
    GROUP BY invNo
) rcpts ON rcpts.invNo = invoice.invNo
LEFT JOIN (
    SELECT invNo, SUM(Gross) AS totCredited
    FROM creditNote
    GROUP BY invNo
) crdts ON crdts.invNo = invoice.invNo
WHERE invoice.Deleted = 0 
AND (Invoice.Gross - IFNULL(crdts.totCredited, 0) - IFNULL(rcpts.totPaid, 0)) >= 0
ORDER BY queryBalance DESC

Open in new window

0
 

Author Closing Comment

by:Lmillard
ID: 39835815
Both great solutions. Thanks very much - they work perfectly!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access Join Query 12 48
Php  POSTGRES  "  IN SIDE "  " 4 39
query question 4 27
query question 13 42
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Creating and Managing Databases with phpMyAdmin in cPanel.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now