Reputation: 16282
How could I change this query bellow to use join instead of a subquery?
SELECT ID, Name, UnitPrice
FROM Products
WHERE UnitPrice <
( SELECT AVG( UnitPrice ) FROM Products )
ORDER BY UnitPrice DESC;
Thanks
Upvotes: 1
Views: 57
Reputation: 81930
Another option is to use the window functions
;with cte as (
SELECT ID
, Name
, UnitPrice
, AvgPrice = avg(UnitPrice) over (Order By (Select NULL))
FROM Products
)
Select * from cte where UnitPrice<AvgPrice
Upvotes: 2
Reputation: 37313
There is a way to do it with join as the following:
SELECT T1.*
FROM [dbo].[Table_1] AS T1
INNER JOIN ( SELECT AVG( Number ) as Number FROM [Table_1] ) AS T2
ON T1.Number < T2.Number
ORDER BY T1.UnitPrice DESC;
Otherwise use variable to store this value
Upvotes: 1
Reputation: 611
You can do it with joins on
or can use:
But your qry is the clearest solution.
Upvotes: 0