If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 12-04-08, 15:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the SUM expression counts all transactions that were over average

the COUNT counts all transactions

when these are equal, this means there were no transactions less than average

per customer

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #17 (permalink)  
Old 12-05-08, 05:45
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
Quote:
Originally Posted by stolze
@JarHL: I don't see how you could possibly interpret the explanations you cited as an argument against subselects in a WHERE clause or SELECT list.

A scalar subselect is an expression. Based on that, you can use subselects in both, a WHERE clause and the SELECT list. Even more so, there are predicates like EXISTS, IN, =ANY, =ALL, etc. that operate on whole sets, i.e. subselects.
Your absolutely right. (I suppose I'd read more carefully before answering.)
Reply With Quote
  #18 (permalink)  
Old 12-05-08, 12:25
khdani khdani is offline
Registered User
 
Join Date: Nov 2008
Posts: 20
I got it now,
Thank you
Reply With Quote
  #19 (permalink)  
Old 12-05-08, 16:48
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by r937
SELECT custid
FROM ord
GROUP
BY custid
HAVING COUNT(*) =
SUM(CASE WHEN total > (SELECT AVG(total) FROM ord)
THEN 1 ELSE NULL END)
Here's yet an other query which returns the required result:
Code:
SELECT custid
  FROM ord, (select avg(total) as average from ord) dv
GROUP BY custid, average
HAVING MIN(total) > average
It's closer to the original query of khdani (with the join to a 1x1 table).
The "MIN(total) >" clearly expresses that all totals within the same group (of orders for the same customer) need to be larger than something.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #20 (permalink)  
Old 12-05-08, 17:59
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
While Peter's solution is elegant, I think that it would fail if there are any NULL values of total, which I would expect to exclude a customer from selection. The query that r937 provided should handle NULL values for total as I think they ought to be handled. If the total column is declared NOT NULL, then obviously this point is moot.

-PatP
Reply With Quote
  #21 (permalink)  
Old 12-05-08, 18:52
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Pat Phelan
While Peter's solution is elegant, I think that it would fail if there are any NULL values of total, which I would expect to exclude a customer from selection.
Yes, you're right.
If I'm not mistaken, that problem could be fixed by replacing MIN(total) by MIN(COALESCE(total,average+1))
Less elegant, of course ...
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On