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

问题描述

使用:

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

解决方法

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