select top 5

  • Thread starter Thread starter AJ
  • Start date Start date
A

AJ

Is there a way to select top 5 (stores) based on a calculation?
example

select * from stores
where ?? in (select top 5 avg(sales + amount))
 
The subquery would need to read from your sales table, and group by the
foeign key.

Perhaps something like this:

select * from stores
where StoreID in
(select top 5 StoreID
FROM Sales
GROUP BY StoreID, avg(sales + amount)
ORDER BY avg(sales + amount) DESC);

More info on subqueries:
http://allenbrowne.com/subquery-01.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top