小编典典

SQL Server-每个状态提取X个随机记录

sql

我有一张表,上面有美国每个邮政编码的记录。为了在地图上显示,我需要为每个州选择X个随机记录。我将如何去做呢?


阅读 198

收藏
2021-05-16

共1个答案

小编典典

使用:

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
2021-05-16