Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Totalling Values

Posted on 2011-02-21
Medium Priority
309 Views
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
Question by:raymurphy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 7

Expert Comment

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

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

lozzamoore earned 2000 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

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

ID: 34943940
Great. Thanks.
0

Featured Post

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses
Course of the Month7 days, 10 hours left to enroll