Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

using ISNULL and SUM on the same column Expand / Collapse
Author
Message
Posted Wednesday, May 30, 2012 7:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 4, 2014 8:50 AM
Points: 16, Visits: 48
I want to generate a report to compare the estimated_total_fee (on the usr_int1 table) with the amount the customer has actually been billed (from ac_billbook.costsnet).

Customers and their orders are identified by matters.entityref for the customer, and matters.number for the order number. Some customers may have had more than one bill generated on the same order; I don't need to know the details of every bill, just the total the customer has been billed for the one order so I'm using the Sum function.

However, I also need to account for orders that have not yet been billed so I want to include IsNull to allow for null values in the Ac_Billbook.costsnet column. I'm not sure how to phrase the syntax to allow for both Sum and IsNull on the same column.

SELECT
Matters.EntityRef,
Matters.Number,
usr_Int1.Estimated_total_fee,
Sum(Ac_Billbook.CostsNet) AS SumOfCostsNet

FROM
Ac_Billbook INNER JOIN
(Usr_Int1 INNER JOIN Matters ON
(Usr_Int1.MatterNo = Matters.Number)
AND (Usr_Int1.EntityRef = Matters.EntityRef))
ON (Ac_Billbook.MatterRef = Usr_Int1.MatterNo)
AND (Ac_Billbook.EntityRef = Usr_Int1.EntityRef)

Any help help gratefully received!
Thanks
Post #1308353
Posted Wednesday, May 30, 2012 7:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
A couple of things:

1. You need a GROUP BY on your query.
2. You can address NULL values for Ac_Billbook.CostsNet 2 ways. You wrap the SUM with an ISNULL, ISNULL(Sum(Ac_Billbook.CostsNet), 0) because SUM ignores nulls when summing or you can reverse the SUM and ISNULL like this, Sum(ISNULL(Ac_Billbook.CostsNet, 0)) which will convert individual NULL's to 0. Doesn't really matter which one you do. The first might perform slightly better because the ISNULL is only being applied once. I've never actually tested it.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1308367
Posted Saturday, August 31, 2013 8:36 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 5:54 PM
Points: 1, Visits: 28
Jack Corbett (5/30/2012)
A couple of things:

1. You need a GROUP BY on your query.
2. You can address NULL values for Ac_Billbook.CostsNet 2 ways. You wrap the SUM with an ISNULL, ISNULL(Sum(Ac_Billbook.CostsNet), 0) because SUM ignores nulls when summing or you can reverse the SUM and ISNULL like this, Sum(ISNULL(Ac_Billbook.CostsNet, 0)) which will convert individual NULL's to 0. Doesn't really matter which one you do. The first might perform slightly better because the ISNULL is only being applied once. I've never actually tested it.


That's not actually true - they are not equivalent.

ISNULL(SUM(Ac_Billbook.CostsNet), 0) should be preferred - SUM will as you say ignore null values. However, if there are zero records to sum over, SUM will return NULL. Most of the time when doing a SUM you would want to treat zero records as having an aggregate sum of zero.
Post #1490435
Posted Tuesday, September 24, 2013 6:40 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, June 23, 2014 4:13 AM
Points: 73, Visits: 333
please add GROUP BY in your query



ISNULL(SUM(Ac_Billbook.CostsNet), 0)
Post #1497815
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse