I have a table with records for each zip code in the united st开发者_如何学运维ates. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this?
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
SELECT * FROM ZipCodes ORDER BY NEWID()
精彩评论