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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With