2010-12-03 33 views
0

在SQL Server 2008中,我使用了列论证和类的表理论。T-SQL选择不同值的示例记录

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 

我想编写select语句,它将返回每个类的两个记录。

非常感谢

回答

3
;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