2010-12-03 9 views
0

In SQL Server 2008 habe ich Tabelle Theorie mit Spalten Thesis und Klasse.T-SQL Beispielsätze unterschiedlicher Werte auswählen

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 

Ich möchte SELECT-Anweisung schreiben, die zwei Datensätze jeder Klasse zurückgibt.

Vielen Dank

Antwort

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 
Verwandte Themen