?
Solved

MySQL select by subquery

Posted on 2014-02-05
3
Medium Priority
?
621 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 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 60

Accepted Solution

by:
Kevin Cross earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 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