MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 27th, 2004, 12:36 AM
stirfry314 stirfry314 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 stirfry314 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Finding values greater than grouped averages

Might be a bit hard to get your head around, but basically, using SQL-99 I have to find birds that are larger than the average height in that family of birds. I'm currently using a subquery that groups all the averages, but I don't know how to then find out what is higher in each family.

select Height, BCommonName
from Birds
where Height > ( select avg(Height)
from Birds
where FCode = Birds.FCode
group by FCode )


This returns nothing.

PLEASE HELP ME.

Reply With Quote
  #2  
Old April 27th, 2004, 12:48 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
I see some errors in your attempt...can you post the table and relevant fields? I have an idea about 'em from your query, but not totally.

Last edited by Username=NULL : April 27th, 2004 at 01:10 AM.

Reply With Quote
  #3  
Old April 27th, 2004, 01:16 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
just gonna take a stab...is this what your table looks like??

Birds
-----
BCommonName //name of the bird?
Height
FCode //family code each bird belongs to?

give this a shot...
Code:
select BCommonName, height
from   Birds B
join  (select  distinct FCode, AVG(height) as avgHeight
       from    Birds
       group   by FCode) as avgQuery
on     B.FCode = avgQuery.Fcode
where  B.height > avgQuery.avgHeight  

Reply With Quote
  #4  
Old April 27th, 2004, 03:05 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
Code:
select Height, BCommonName
from Birds b
where Height > ( select avg(Height)
from Birds
where FCode = B.FCode)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Finding values greater than grouped averages


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway