2

I have a three column table and the column headers are: place of work, place of residence and number of commuters (geocode, geocode, number). For each place of work, I would like to know the top 5 most common places of residence along with the number of commuters. Any ideas on how to do this in SQL?

4

1 回答 1

2

不确定您使用的是哪个 RDBMS,但这应该适用于 SQL Server:

with MyCTE AS
(
    select *, RANK() OVER (PARTITION BY placeOfWork ORDER BY NumberOfCommuters DESC) AS Rank
    from PlacesOfWorkWork
)
select * from MyCTE where Rank <= 5
于 2012-10-09T18:10:39.130 回答