开发者

T-SQL select sample records of distinct values

开发者 https://www.devze.com 2023-01-28 13:55 出处:网络
In SQL server 2008 I have table Theory with columns Thesis and Class. ThesisClass <this is sample text>1

In SQL server 2008 I have table Theory with columns Thesis and Class.

Thesis                   Class
<this is sample text>    1
<this is sample text>    2
<this is sample text>    2
<this is sample text>    3
<this is sample text>    开发者_开发问答3
<this is sample text>    1
<this is sample text>    3
<this is sample text>    1
<this is sample text>    1
<this is sample text>    2

I want to write select statement that will return two records of each class.

Many thanks


;WITH T AS
(
SELECT Thesis, 
       Class, 
       ROW_NUMBER() OVER (PARTITION BY Class ORDER BY (SELECT 0) ) rn
FROM Theory 
)
SELECT Thesis, Class
FROM T 
WHERE rn <=2
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号