-1

以下查询返回前 24 个随机记录,如下所示

SELECT top 24
    ROW_NUMBER() OVER (ORDER BY QuestionID) AS QuestionNo,
    Q.QuestionID,
    CS.Section
FROM Questions Q
left join dbo.ChapterSection CS
    on Q.SectionID=CS.SectionID
order by newid()

74  656      Section 3.1 - A long and illustrious history- Early Briton
183 765      Section 3.3 - A long and illustrious history-The Tudors and Stuarts
432 1017     Section 4.5 - A modern, thriving society - Arts and culture

我想要的是 QuestionNo 的值从 1 到 24 如下

1   656     Section 3.1 - A long and illustrious history- Early Briton
2   765     Section 3.3 - A long and illustrious history-The Tudors and Stuarts
3   1017    Section 4.5 - A modern, thriving society - Arts and culture

有任何想法吗?谢谢

4

1 回答 1

2
SELECT *, ROW_NUMBER() OVER (ORDER BY ni) QuestionNo
FROM
    (SELECT TOP 24 *, newid() ni
    FROM Table
    order by newid()
    )t 

或者

SELECT ROW_NUMBER() OVER (ORDER BY rn), *
FROM
    (SELECT TOP 24 *, ROW_NUMBER() OVER (ORDER BY newid()) rn
    FROM Table
    ) t
于 2013-11-14T23:39:52.767 回答