Sorry to be long winded but I want to make sure that everyone understand the issue I am having. So please bear with me.
I have 2 tables, ie. Filter_Clients_Invoices_Entries and Filter_Clients_Invoices_Payments.
Please refer to the query and results
SELECT
FCIE.InvoiceID,
FCIE.EntryID,
FCIE.ClientID,
CAST(FCIE.AddedOn AS DATE) AS EntryDate,
FCIE.ItemTotal,
SUM(IFNULL(FCIP.PaymentAmount,0)) AS Payments
FROM Filter_Clients_Invoices_Entries FCIE
LEFT JOIN Filter_Clients_Invoices_Payments FCIP ON FCIP.EntryID = FCIE.EntryID
AND CAST(FCIP.PaymentDate AS DATE) BETWEEN '2014-10-01' AND '2014-11-30'
WHERE FCIE.ClientID = 3693
AND CAST(FCIE.AddedOn AS DATE) BETWEEN '2014-10-01' AND '2014-11-30'
AND FCIE.Void = 0
GROUP BY FCIE.EntryID;
The results are as follows
+-----------+---------+----------+------------+-----------+----------+
| InvoiceID | EntryID | ClientID | EntryDate | ItemTotal | Payments |
+-----------+---------+----------+------------+-----------+----------+
| 31327 | 34180 | 3693 | 2014-10-31 | 63.30 | 33.30 |
| 31327 | 34181 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34182 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34183 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34184 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34185 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31331 | 34187 | 3693 | 2014-11-03 | 15.00 | 0.00 |
| 31332 | 34188 | 3693 | 2014-11-04 | 100.00 | 100.00 |
| 31341 | 34197 | 3693 | 2014-11-10 | 85.00 | 0.00 |
| 31342 | 34198 | 3693 | 2014-11-10 | 85.00 | 0.00 |
| 31343 | 34199 | 3693 | 2014-11-12 | 95.00 | 0.00 |
| 31344 | 34200 | 3693 | 2014-11-13 | 85.00 | 0.00 |
| 31345 | 34201 | 3693 | 2014-11-13 | 80.00 | 0.00 |
| 31346 | 34202 | 3693 | 2014-11-26 | 80.00 | 0.00 |
| 31347 | 34203 | 3693 | 2014-11-26 | 80.00 | 0.00 |
| 31348 | 34204 | 3693 | 2014-11-26 | 100.00 | 0.00 |
+-----------+---------+----------+------------+-----------+----------+
16 rows in set (0.96 sec)
What I wanted to do was to only retrieve item records that has balance, so this is the modified query
SELECT
FCIE.InvoiceID,
FCIE.EntryID,
FCIE.ClientID,
CAST(FCIE.AddedOn AS DATE) AS EntryDate,
FCIE.ItemTotal,
SUM(IFNULL(FCIP.PaymentAmount,0)) AS Payments
FROM Filter_Clients_Invoices_Entries FCIE
LEFT JOIN Filter_Clients_Invoices_Payments FCIP ON FCIP.EntryID = FCIE.EntryID
AND CAST(FCIP.PaymentDate AS DATE) BETWEEN '2014-10-01' AND '2014-11-30'
WHERE FCIE.ClientID = 3693
AND CAST(FCIE.AddedOn AS DATE) BETWEEN '2014-10-01' AND '2014-11-30'
AND FCIE.Void = 0
GROUP BY FCIE.EntryID
HAVING SUM(IFNULL(FCIP.PaymentAmount,0)) < FCIE.ItemTotal;
The new results are as follows
+-----------+---------+----------+------------+-----------+----------+
| InvoiceID | EntryID | ClientID | EntryDate | ItemTotal | Payments |
+-----------+---------+----------+------------+-----------+----------+
| 31327 | 34180 | 3693 | 2014-10-31 | 63.30 | 33.30 |
| 31327 | 34181 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34182 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34183 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34184 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31327 | 34185 | 3693 | 2014-10-31 | 63.30 | 0.00 |
| 31331 | 34187 | 3693 | 2014-11-03 | 15.00 | 0.00 |
| 31341 | 34197 | 3693 | 2014-11-10 | 85.00 | 0.00 |
| 31342 | 34198 | 3693 | 2014-11-10 | 85.00 | 0.00 |
| 31343 | 34199 | 3693 | 2014-11-12 | 95.00 | 0.00 |
| 31344 | 34200 | 3693 | 2014-11-13 | 85.00 | 0.00 |
| 31345 | 34201 | 3693 | 2014-11-13 | 80.00 | 0.00 |
| 31346 | 34202 | 3693 | 2014-11-26 | 80.00 | 0.00 |
| 31347 | 34203 | 3693 | 2014-11-26 | 80.00 | 0.00 |
| 31348 | 34204 | 3693 | 2014-11-26 | 100.00 | 0.00 |
+-----------+---------+----------+------------+-----------+----------+
15 rows in set (0.95 sec)
Although the query works, you can see that it took nearly 1 second to complete the query ... and this is only for 1 client (WHERE ClientID = 3693).
If I were to remove that ClientID filter, it will return me 106 records but takes 58 SECONDS to complete.
So I am asking help from all MySQL guru out there for the following questions
- Can the query be optimized to get the same result with faster execution time
- Help modify the query to group by InvoiceID with total sum of ItemTotal
Thanks.