我有一张表,其中包含美国各邮政编码的记录。为了在地图上显示,我需要为每个州选择X个随机记录。我该怎么做呢?
答案 0 :(得分:12)
使用:
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
答案 1 :(得分:0)
SELECT * FROM ZipCodes ORDER BY NEWID()