Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top XX list based on how many rows contain XX

Status
Not open for further replies.

ryan101

Programmer
Jul 11, 2001
13
US
I have a table with the following 3 columns:
Ticket_Number, Location, date_created

I need to return a top 3 list that contains the locations sorted by the count of each location.

For instance, say I have 10 rows total. 4 rows have "Main Office" for location, 3 have "Satellite Office", 2 have "Telecom Shop". I want to have a list that contains 3 single column rows with the names of the top 3 locations(Main Office, Satellite Office, Telecom Shop)

I can do this using multiple recordsets in VB, but I'd like to be able to do it in a single query. Any ideas?
 
answer dawned on me a while after I submitted. If anyone's interested, the command I used that works is:

SELECT LOCATION, TIMES FROM(
SELECT LOCATION, COUNT(LOCATION) TIMES
FROM TABLE_NAME
GROUP BY LOCATION
ORDER BY TIMES DESC
)
WHERE ROWNUM < 6

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top