Solved

Totalling Values

Posted on 2011-02-21
5
293 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
[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
  • Learn & ask questions
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What type of testing am I doing? 4 76
T-SQL: Wrong Result 7 39
When are cursors useful? 8 62
Migrate SQL 2005 DB to SQL 2016 4 32
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

751 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