2014-12-02 60 views
0

首先,對不起我的英文不好。我在創建SQL語句時遇到了問題。對同一列結果的SQL計數行數

我創建了一個行數列:

SELECT 
    ROW_NUMBER() OVER(ORDER BY KNR DESC) AS Row, 
    KNR, text, DATUM 
FROM 
    KURSTAGE 
WHERE 
    (KNR like '%E3%') AND (TEXT = 'TEXT') AND (datum >= '02.12.2014') AND (KNR like 'O%') 

結果看起來是這樣的:

Row Result1 Result2 etc. 
------------------------------------------------ 
1 OE3WU9B TestTest 2015-06-28 00:00:00.000 
2 OE3WU9B TestTest 2015-06-28 00:00:00.000 
3 OE3WU9B TestTest 2015-06-07 00:00:00.000 
4 OE3WU9B TestTest 2015-05-30 00:00:00.000 
5 OE3WU9B TestTest 2015-05-10 00:00:00.000 
6 OE3ST9B TestTest 2015-05-31 00:00:00.000 
7 OE3ST9B TestTest 2015-05-17 00:00:00.000 
8 OE3ST9B TestTest 2015-05-10 00:00:00.000 
9 OE3ST9B TestTest 2015-04-26 00:00:00.000 
10 OE3ST9B TestTest 2015-04-19 00:00:00.000 

是否有可能創造出開始從1同時開始計算行數來自Result1的結果?

例如:

Row Result1 Result2 etc. 
1 OE3WU9B TestTest 2015-06-28 00:00:00.000 
2 OE3WU9B TestTest 2015-06-28 00:00:00.000 
3 OE3WU9B TestTest 2015-06-07 00:00:00.000 
4 OE3WU9B TestTest 2015-05-30 00:00:00.000 
5 OE3WU9B TestTest 2015-05-10 00:00:00.000 
**1** OE3ST9B TestTest 2015-05-31 00:00:00.000 
2 OE3ST9B TestTest 2015-05-17 00:00:00.000 
3 OE3ST9B TestTest 2015-05-10 00:00:00.000 
4 OE3ST9B TestTest 2015-04-26 00:00:00.000 
5 OE3ST9B TestTest 2015-04-19 00:00:00.000 

回答

0
SELECT ROW_NUMBER() OVER 
(PARTITION BY KNR ORDER BY KNR,DATUM DESC) AS Row, 
KNR, 
text, 
DATUM FROM KURSTAGE 
WHERE (KNR like '%E3%') 
AND (TEXT = 'TEXT') 
AND (datum >= '02.12.2014') 
AND (KNR like 'O%') 
+0

偉大的,非常感謝你,你讓我很快樂:) – tearitdown 2014-12-02 08:26:51