views:

17

answers:

2

Hi I have one "orders" table:

ID  CustomerID  Freight
1      VINET      32.38
2      TOMPS      11.61
.        .          .
.        .          . 
.        .          .

I want to select just those customerID s that have freight more than average!!

SELECT CustomerID FROM Orders WHERE CustomerID IN(SELECT CustomerID,AVG(Freight) AVGFreight FROM Orders Group By CustomerID )

I know till here but how can I compare freight with the average!?? please help me thanks

A: 

How about this:

SELECT CustomerID 
FROM dbo.Orders 
WHERE Freight >= (SELECT AVG(Freight) FROM dbo.Orders)
GROUP BY CustomerID

You select your customers, all of which have a freight cost which is larger than the average of all freight costs in the table.

marc_s
I should just select the Customer ID
it will send an error !! error :*Incorrect syntax near '='.*
aha this one will find the avarage of each customerID and then will check the freight of each customer ID with its average?
@Matin1234: it will calculate the average freight over all entries of the table - then select all customers which have a freight value higher than that average
marc_s
+1  A: 

I am assuming that a customer will have 1 record each.

SELECT CustomerID FROM dbo.Orders WHERE Freight >= (SELECT @AvgFreight = AVG(Freight) FROM dbo.Orders)

I am not sure, if this will work. Try it.
Upvote @marc_s's answer, as I am basing it off his reply.

SELECT CustomerID
FROM dbo.Orders 
WHERE AVG(Freight) >= (SELECT AVG(Freight) FROM dbo.Orders)
GROUP BY CustomerID
shahkalpesh
no Customer ID is not unique!
Please explain what you are looking for, with sample input as well as output data.
shahkalpesh