Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL select by subquery

Posted on 2014-02-05
3
Medium Priority
?
619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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 series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…

636 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