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 > Need help with a simple set function query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-27-04, 23:48
Solus Solus is offline
Registered User
 
Join Date: Apr 2004
Posts: 7
Question Need help with a simple set function query

I am trying to find all materials with a length greater than the average length of all the materials put together to make the category.

Can anybody help me?
This is what I tried so far but it says Length has to be in the group by. If I do that I don't think it will return what I want.


Code:
Select Name, Length
from materials
group by category
having Length > (Select avg(Length) from materials)

Thanks in advance.
-Solus
Reply With Quote
  #2 (permalink)  
Old 04-28-04, 00:10
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
SELECT
m1.Name,
m1.Length
FROM
materials m1
INNER JOIN (
SELECT
AVG(Length) AS Length,
Category FROM materials) m2 ON m1.Category = m2.Category
AND m1.Length > m2.Length
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #3 (permalink)  
Old 04-28-04, 00:28
Solus Solus is offline
Registered User
 
Join Date: Apr 2004
Posts: 7
That didn't work Came up with Circular reference for Length
Reply With Quote
  #4 (permalink)  
Old 04-28-04, 00:42
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
If you are using SQL Server, this works:

-- CREATE TABLE materials(
-- Name VARCHAR(55),
-- Category VARCHAR(55),
-- Length INT)
--
-- INSERT materials(Name,Category,Length)
-- SELECT '2x4','lumber',10
-- UNION ALL
-- SELECT '2x4','lumber',20
-- UNION ALL
-- SELECT '2x4','lumber',30
-- UNION ALL
-- SELECT '4x8','lumber',10
-- UNION ALL
-- SELECT '4x8','lumber',20
-- UNION ALL
-- SELECT '4x8','lumber',30
--


SELECT
m1.Name,
m1.Length
FROM
materials m1
INNER JOIN (
SELECT
AVG(Length) AS Length,
Category
FROM materials
GROUP BY Category) m2 ON m1.Category = m2.Category
AND m1.Length > m2.Length

If you are not using SQL Server, what database are you using?
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #5 (permalink)  
Old 04-28-04, 00:59
Solus Solus is offline
Registered User
 
Join Date: Apr 2004
Posts: 7
MS access 2003
Reply With Quote
  #6 (permalink)  
Old 04-28-04, 01:13
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
This is why I hate Access. Thanks for reminding me. lol

SELECT materials.Name, materials.Category, materials.Length
FROM materials INNER JOIN materials AS materials_1 ON materials.Category = materials_1.Category
GROUP BY materials.Name, materials.Category, materials.Length
HAVING (((Avg(materials_1.Length))<[materials].[Length]));
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #7 (permalink)  
Old 04-28-04, 01:23
Solus Solus is offline
Registered User
 
Join Date: Apr 2004
Posts: 7
isn't inner join a noise word (Meaning it does nothing) sorta like "order by X asc". I'm just new to SQL and wonder why people include noise words. The only reason I could think of is that maybe some DBMS's don't work normally or as you would expect without them.
Reply With Quote
  #8 (permalink)  
Old 04-28-04, 03:09
Solus Solus is offline
Registered User
 
Join Date: Apr 2004
Posts: 7
thx for the help
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