SQL counting row number for every same column result

139 views Asked by At

First of all, sorry for my bad English. I have a problem creating a SQL statement.

I created a row count column:

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%') 

The result looks like this:

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

Is it possible to create a ROW count which starts counting from 1 at the start of the same result from Result1?

For example:

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
1

There are 1 answers

0
Raj On BEST ANSWER
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%')