0

i dont have much expiriance with SQL and i am trying to crack my head on this query. i have 3 tables: Projects, Calculator and partialBilling (note: the 'calculator' columns you see at the code ive added 'k','l','m' etc are real...i didnt gave them those names...).

the query is working fine but part of the values that i am expecting from the aggregate function ('sumofTotal' column) are returning as null values and and they should not be null. I would be grateful if someone point out the mistake in the query.

SELECT Projects.SpCall,Projects.CustName,Projects.CustNumber
,Projects.ReceiveDate,Projects.StartDate,Projects.ProjectType
,Calculator.AN,Projects.Professional,Projects.PmUserName
,Projects.AcountManager,Projects.CrmCallNum,Projects.ProjectCategory
,Projects.CallNum,Projects.ContactName,Projects.ContactPhone
,Projects.ContactEmail,Projects.HiddenNote,Projects.RowColor
, Projects.HeaderCellText,
 SUM(Calculator.K + Calculator.L + Calculator.M + Calculator.N + Calculator.AD + Calculator.AR) AS sumofTotal
 ,partialBilling.Ammount FROM Projects LEFT JOIN Calculator ON Projects.SpCall=Calculator.AQ
  LEFT JOIN partialBilling ON Projects.SpCall = partialBilling.spCall
   WHERE PmUserName= 'JOHN DOE'AND OpertionalStatus
    <> 'Billed' AND OpertionalStatus<> 'Finished' AND
     OpertionalStatus<> 'Passed To Billing' AND OpertionalStatus<> 'Scanning' 
     AND OpertionalStatus<> 'Ended' 
     AND OpertionalStatus<> 'Green Billing' 
     AND (GeneralStatus= 'Passed To Project Manager' 
     OR GeneralStatus= 'Astrategic Project') 
     GROUP BY Projects.SpCall,Projects.CustName,Projects.CustNumber
     ,Projects.ReceiveDate,Projects.StartDate,Projects.ProjectType
     ,Calculator.AN,Projects.Professional,Projects.PmUserName
     ,Projects.AcountManager,Projects.CrmCallNum,Projects.ProjectCategory
     ,Projects.CallNum,Projects.ContactName,Projects.ContactPhone
     ,Projects.ContactEmail,Projects.HiddenNote,Projects.RowColor
     , Projects.HeaderCellText,partialBilling.Ammount;
2
  • 2
    Simplify the problem and it will be easier for everybody (incl you) to understand it. Much less columns, some sample table data, and the expected result. Commented Apr 5, 2016 at 14:03
  • tnx, working on it. few minuets. Commented Apr 5, 2016 at 14:06

3 Answers 3

1

Instead of proprietary IFNULL better use Standard SQL COALESCE:

SUM(COALESCE(Calculator.K,0) + COALESCE(Calculator.L,0), ...`

Or maybe a bit more efficient:

SUM(COALESCE(Calculator.K,0)) + SUM(COALESCE(Calculator.L,0)), ...`
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use IFNULL()

SUM(IFNULL(Calculator.K,0) + ... + IFNULL(Calculator.AR,0)) AS sumofTotal

Comments

0

You can use an expression like ifnull('column_name' , '') in place of column_name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.