Solved

Totalling Values

Posted on 2011-02-21
5
286 Views
Last Modified: 2012-05-11
Given my two tables of SupplierNames and SupplierLevels (stripped down here for legibility) :

        SupplierNames
SupNo   SupName
1      Stevenson Supplies
2      Jolson Supplies
3      Bobbington Supplies
4      Juleen Supplies
5      Harington Supplies
6      Freed Supplies
7      Gillingham Supplies

       SupplierOrderHistory
SupOrders    SupNo
300           4
120           6
12           3
140           6
543           1
455           2
311           3
555           6
234           5

Need to see a list of Suppliers that do NOT have cumulative orders (i.e. the total of their SupOrders in SupplierOrderHistory) of at least 450 - need to include Gillingham Supplies (SupNo 7) in this list even though they haven't placed any orders yet.
SupNo on SupplierOrderHistory table matches SupNo on SupplierNames, and data is on SSQL 2005 database.

All assistance appreciated ....
0
Comment
Question by:raymurphy
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:lozzamoore
ID: 34943551
Simplest way would be something like this:

SELECT sn.SupName, SUM(sh.SupOrders)
FROM SupplierNames sn
LEFT OUTER JOIN dbo.SupplierOrderHistory sh ON sn.SupNo = sh.SupNo
GROUP BY sn.SupName
HAVING COALESCE(SUM(sh.SupOrders), 450)>=450
0
 

Author Comment

by:raymurphy
ID: 34943716
This looks fine, so I'll test in a few mins to confirm, but would just like to understand what the COALESCE part of the HAVING clause does in this particular case ?
0
 
LVL 7

Accepted Solution

by:
lozzamoore earned 500 total points
ID: 34943809
Sure,
The COALESCE function is very similar to ISNULL, only the former is ANSI compliant.
This converts the NULL returned for no SH records found (e.g. Gillingham Supplies) to some value specified. In this case, we convert to 450 so that it also matches the filter criteria.

Make sense?
Kind regards,
L

PS If ANSI compliance is not an issue, generally ISNULL is preferable from a performance point of view.
http://sqlserverperformance.idera.com/tsql-optimization/performance-coalesce-null/
0
 

Author Closing Comment

by:raymurphy
ID: 34943869
That makes sense now I've read it over again, so thanks for an excellent solution and explanation - and it works fine as I've just tested it ....
0
 
LVL 7

Expert Comment

by:lozzamoore
ID: 34943940
Great. Thanks.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question