SQL Server - pull X random records per state
问题 I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this? 回答1: Use: WITH sample AS ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.state ORDER BY NEWID()) AS rank FROM ZIPCODES t) SELECT s.* FROM sample s WHERE s.rank <= 5 回答2: SELECT * FROM ZipCodes ORDER BY NEWID() 来源: https://stackoverflow.com/questions/3998573/sql-server-pull-x-random-records-per-state