2010-12-03 6 views
0

Dans SQL Server 2008, j'ai la table Théorie avec des colonnes Thèse et classe.T-SQL sélectionner des enregistrements d'échantillons de valeurs distinctes

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 

Je veux écrire une instruction select qui retournera deux enregistrements de chaque classe.

Un grand merci

Répondre

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 
Questions connexes