Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL giving me 0

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2003-10-15 : 14:36:28
The following sql works fine if there are payment records in the payment table, but if there are no records there, the AmountOwed = 0. How do I deal with the null?
"SUM(paymentAmount) FROM Payment" will be the only table that may not have records




SELECT (SELECT SUM(SalesPrice)
FROM tempSalesDetail
WHERE (tempsales.tempSalesID = tempSalesDetail.tempSalesID)) -
(SELECT SUM(paymentAmount)
FROM Payment
WHERE (tempsales.tempSalesID = Payment.tempSalesID)) + tempSales.totalShipping + tempSales.taxableAmount AS AmountOwed,
tempSales.tempSalesID, Contacts.FIRST_NAME + ' ' + Contacts.LAST_NAME AS theName, company.COMPANY,
Users.First_Name + ' ' + Users.Last_Name AS userName, tempSales.SalesDate
FROM tempSales INNER JOIN
Contacts ON tempSales.CONTACT_ID = Contacts.CONTACT_ID INNER JOIN
company ON Contacts.COMPANY_ID = company.COMPANY_ID INNER JOIN
Users ON tempSales.sentByID = Users.UserID
WHERE (tempSales.SalesActive = 1) AND (tempSales.salesStatusID = 1)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-15 : 14:40:25
You could use ISNULL:

SELECT ISNULL(SomeColumn, 0)
FROM SomeTable

So instead of a NULL, you'll get 0.

Tara
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-10-15 : 15:00:58
This may not be a NULL problem but just a problem of eliminating AmountOwed when it's zero?


SELECT AmountOwed, TempSalesID, TheName, Company, UserName, SalesDate
FROM (
SELECT (SELECT SUM(SalesPrice) FROM tempSalesDetail
WHERE (tempsales.tempSalesID = tempSalesDetail.tempSalesID)) -
(SELECT SUM(paymentAmount) FROM Payment
WHERE (tempsales.tempSalesID = Payment.tempSalesID)) +
tempSales.totalShipping + tempSales.taxableAmount AS AmountOwed,

tempSales.tempSalesID,
Contacts.FIRST_NAME + ' ' + Contacts.LAST_NAME AS theName,
company.COMPANY,
Users.First_Name + ' ' + Users.Last_Name AS userName,
tempSales.SalesDate
FROM tempSales
INNER JOIN Contacts ON tempSales.CONTACT_ID = Contacts.CONTACT_ID
INNER JOIN company ON Contacts.COMPANY_ID = company.COMPANY_ID
INNER JOIN Users ON tempSales.sentByID = Users.UserID
WHERE (tempSales.SalesActive = 1) AND (tempSales.salesStatusID = 1)
)
X
WHERE X.AmountOwed <> 0



Go to Top of Page
   

- Advertisement -