开发者

T-SQL select sample records of distinct values

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜