一尘不染

SQL Server选择排名前10-20的结果?

sql

我有两列,第一列是我想要排名前10位的产品(1-10)

那是

SELECT TOP 10 * FROM Product

在第二列中,我想要接下来的10个结果(11-20)

我怎么做?


阅读 522

收藏
2021-03-17

共1个答案

一尘不染

WITH T AS
(
SELECT TOP 20 name, 
       row_number() OVER (ORDER BY id) AS RN
FROM Products
ORDER BY id
)
SELECT 
       MAX(CASE WHEN RN <=10 THEN name END) AS Col1,
       MAX(CASE WHEN RN > 10 THEN name END) AS Col2
FROM T       
GROUP BY RN % 10
2021-03-17