Solved

Totalling Values

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

867 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

20 Experts available now in Live!

Get 1:1 Help Now