Solved

MySQL select by subquery

Posted on 2014-02-05
3
607 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

18 Experts available now in Live!

Get 1:1 Help Now