I want to generate a sequence for a complex query. For that purpose I have used a variable @rowNo.
My logic is :-
If a field - isremoved = 0 then increase row number by 1.
If isremoved = 1 then increase row number by 1 only once till you find next 0.
This is so far I have done, but it gives me syntax error.
DECLARE @rowNo INT;
SET @rowNo = -1;
SELECT
case when sampleTable.isremoved = 0 then @rowNo + 1
else @rowNo end
as rowNumber,
X,
Y,
Z
.
.
FROM tbl_sample sampleTable
INNER JOIN tbl_sample_2 sample2 ON sampleTable.id = sample2.id
.
.
.
This is my desire output :-
So what is the right way to achieve this kind of functionality in sql server 2012 ?
EDIT :- I do have one solution to use sub query to retrieve row number. But that will hit performance as it is very complex query (more than 20 joins) with huge amount of data. So please suggest me an alternative.
