Solved

Totalling Values

Posted on 2011-02-21
5
273 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now