473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with LEFT JOIN + SUM(IF())

I'm trying to get a monthly total of invoices and turnover (from table 'invoices') and the total payments for those invoices (from table 'payments').

This is the query that I use:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   COUNT(i.inv_nr) as nbr_inv, 
  3.   SUM(IF(i.cancelled='0',i.total_amount,0)) as turnover, 
  4.   DATE_FORMAT(i.date_sign,'%m %Y') AS month, 
  5.   SUM(IF(i.cancelled='1',1,0)), 
  6.   SUM(p.amount) 
  7. FROM invoices as i 
  8. LEFT OUTER JOIN payments as p 
  9.   ON i.inv_nr=p.inv_nr 
  10. WHERE i.type != '7' 
  11.   AND month = '$g_b_month' 
  12.   AND i.inv_nr IN (
  13.     SELECT inv_nr from invoice_details) 
  14.   AND i.unit_ID='$util_ID' 
  15. GROUP BY month
  16.  
The problem is that some payments are total, or partial, or missing. When the payments are multiple for the same invoice, it multiplies the counted number of invoice('nbr_inv') and the turnover.

Does anyone have an idea?
May 16 '07 #1
0 1668

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: StealthBananaT | last post by:
My database has two tables - films has 10,000 records and reviews has 20,000 records. Whenever I try to list all the films and the count of its reviews, MySQL locks and I have to restart the...
0
by: Marek Lewczuk | last post by:
Hello, I have a strange problem, maybe some of you will be able to explain me something. I use LEFT JOIN as a substitute for subselects. It's true that many subselects can be rewriten using LEFT...
2
by: michael | last post by:
Gotta post because this is driving me nuts. Trying to DELETE orphans. I can successfully: SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID IS NULL; but when I...
0
by: Jon Trelfa | last post by:
I'm having difficulties getting all rows to return from the left table on a left join. For some reason, i only get the rows where there is a match between the right and the left table. Here's...
5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
2
by: Vector | last post by:
I have this working fine in Access and MSSQL. While trying to convert to MySQL the result is not what I'm looking for to get.. Two tables: T1 ---------------- ID1|cName |...
1
by: Eric | last post by:
Hi Folks, Lets assume I have three tables. Their layout is as follows. Please note that tblPeople does not have an entry for Denver (this is my problem) tblCity _________________ CityName ...
1
by: Quarco | last post by:
Hi, Suppose I have a query like: SELECT products.name AS product, SUM(IF(stock.invoice=0,1,0)) AS in_stock, SUM(IF(shopcart.status=1,1,0)) AS reserved FROM products LEFT JOIN stock ON...
7
by: deko | last post by:
SELECT tblTxAcct.TxAcctName, tblTxType.TxTypeName, Nz(qryTxAcctListCt.TxCount, 0) AS TxCt FROM (tblTxAcct INNER JOIN tblTxType ON tblTxAcct.TxType_ID=tblTxType.TxType_ID) LEFT JOIN qryTxAcctListCt...
3
by: rrstudio2 | last post by:
If I have two tables and need to do a left outer join and include a where statement on the second table, it seems like the left outer join becomes an inner join. For example: Table: Names id...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.