Suppose I have a table with the fields "location", daily_sales", and "date". There are 3 different locations and I want the Top 3 "daily_sales" for each location. I know this can easily be done using separate queries for each location (i.e., Select Top 3 Daily_Sales from Sales where location = "A" Order By Daily_Sales DESC). Unfortunately, there are actually thousands of "locations" in my database and using separate queries doesn't seem to be very scalable. I've made various attempts using grouping, query of queries, and nested queries but can't seem to get the results I need. This seems like a simple enough problem and I'm hoping there is a simple solution that I am just overlooking. Any suggestions would be greatly appreciated.
Problem solved. Someone pointed me in the right direction. Below is the query that produces what I needed. select location , Daily_Sales from Sales as T where ( select count(*) from Sales where location = T.location and Daily_Sales > T.Daily_Sales ) < 3 order by location , Daily_Sales desc Code (markup):