SQL query: calculation not working


Can anybody telle why the following part doesn't work?
Here the VerzendStatus will always be 0.
CASE WHEN ((AantalTotaal - (AantalVerzonden + AantalDoublures)) = 0) THEN 1 ELSE 0 END AS VerzendStatus

Open in new window

SELECT ISNULL(MOTot.AantalTotaal, 0) AS AantalTotaal 
  , ISNULL(MO2.AantalVerzonden, 0) AS AantalVerzonden
  , ISNULL(MO3.AantalDoublures, 0) AS AantalDoublures 
  , CASE WHEN ((AantalTotaal - (AantalVerzonden + AantalDoublures)) = 0) THEN 1 ELSE 0 END AS VerzendStatus
dbo.Medewerkers ON dbo.Mailings.Gebruikersnr = dbo.Medewerkers.Persoonsnr 
FROM dbo.MailingsDetails
GROUP BY MailingID) AS MOTot ON dbo.Mailings.MailingID = MOTot.MailingID 
LEFT OUTER JOIN (SELECT MailingID, COUNT(*) AS AantalVerzonden
FROM dbo.MailingsDetails AS MailingsDetails_4
WHERE (VerzendStatus = 2)
GROUP BY MailingID) AS MO2 ON dbo.Mailings.MailingID = MO2.MailingID 
LEFT OUTER JOIN (SELECT MailingID, COUNT(*) AS AantalDoublures
FROM dbo.MailingsDetails AS MailingsDetails_3
WHERE (VerzendStatus = 3)
GROUP BY MailingID) AS MO3 ON dbo.Mailings.MailingID = MO3.MailingID

Open in new window

Stef MerlijnDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do any of the three values in the calculation contain a null?  If so it will always be false.  

Stef MerlijnDeveloperAuthor Commented:
Doesn't this cover that?
ISNULL(MO3.AantalDoublures, 0) AS AantalDoublures
You have to put ISNULL around the fields in the calculation too.

((ISNULL(AantalTotaal,0) - (ISNULL(AantalVerzonden,0) + ISNULL(AantalDoublures,0))) = 0)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
i would try:
CASE WHEN ((isnull(AantalTotaal, 0) - (isnull(AantalVerzonden, 0) + isnull(AantalDoublures,0))) = 0) THEN 1 ELSE 0 END AS VerzendStatus
Stef MerlijnDeveloperAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.