Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I have to arrive rownumber for following conditions

C#
User Month Lmonth ARR RN
A1 200810 200902 1 0
A1 200811 200902 1 0
A1 200812 200902 2 0
A1 200901 200902 3 0
A1 200902 200902 3 1
A1 200903 200902 4 2
A1 200904 200902 4 3
A1 200905 200902 0 0
A1 200906 200902 1 1
A1 200907 200902 2 2
B1 200905 200908 0 0
B1 200906 200908 1 0
B1 200907 200908 2 0
B1 200908 200908 2 1
B1 200909 200908 2 2
B1 200910 200908 0 0
B1 200911 200908 1 1
B1 200912 200908 1 2


In my above table I have to arrive RN column. The conditions are
1. RN should starts when Month >= LMonth
2. RN should be 0 If ARR = 0 and RN restarts from 1 when ARR > 0

What I have tried:

select User,Month,Lmonth,ARR,ROW_NUMBER() OVER (PARTITION BY User ORDER BY Month)RN FROM MyTable WHERE Month >= LMonth
UNION ALL
select User,Month,Lmonth,ARR,0 FROM MyTable WHERE Month < LMonth
order by 1,2

Iam struggling with second condition.
Posted
Updated 15-Jun-16 11:18am
v2
Comments
Maciej Los 15-Jun-16 16:00pm    
What is expected output?
Is there any primary key?
kirthiga S 16-Jun-16 0:57am    
RN column is my expected output. For each User Month should be unique.
Ex. For User A1 Month starts from 200810 to 200907.

You have already been given an answer in your original of this question at How to generate rownumber with specific conditions[^].

If you need further help then reply to the solution in that question, rather than reposting.
 
Share this answer
 
Comments
CHill60 15-Jun-16 11:07am    
I advised him to post a separate question - if you note on the other post there is no ARR column - OP wishes the Row Numbering to restart when ARR=0 is encountered.
The solution posted at the other post does not take into account column ARR (which was not mentioned in the original post - see the comments)

tl;dr; This is a subtly different problem to the one on the link
kirthiga S 16-Jun-16 0:59am    
Both questions are different
First of all, please read my comment to the question...

In my opinion, there's few ways to achieve that.

One of them is to create custom row numbering using CTE[^].

Another one is to use ROW_NUMBER() function:

SQL
DECLARE @tmp TABLE ([User] VARCHAR(10), [Month] INT, Lmonth INT, ARR INT)

INSERT INTO @tmp ([User], [Month], [Lmonth], ARR)
VALUES('A1',200810, 200902, 1),
('A1',200811, 200902, 1),
('A1',200812, 200902, 2),
('A1',200901, 200902, 3),
('A1',200902, 200902, 3),
('A1',200903, 200902, 4),
('A1',200904, 200902, 4),
('A1',200905, 200902, 0),
('A1',200906, 200902, 1),
('A1',200907, 200902, 2),
('B1',200905, 200908, 0),
('B1',200906, 200908, 1),
('B1',200907, 200908, 2),
('B1',200908, 200908, 2),
('B1',200909, 200908, 2),
('B1',200910, 200908, 0),
('B1',200911, 200908, 1),
('B1',200912, 200908, 1)

SELECT [User], [Month], [Lmonth], ARR, 0 AS RN
FROM @tmp
WHERE  [Month]<[Lmonth] OR ARR = 0
UNION ALL
SELECT [User], [Month], [Lmonth], ARR, ROW_NUMBER() OVER(PARTITION BY [User] ORDER BY [Month]) AS RN
FROM @tmp
WHERE  [Month]>=[Lmonth] AND ARR > 0


Result:
User	Month	Lmonth	ARR	RN
A1		200810	200902	1	0
A1		200811	200902	1	0
A1		200812	200902	2	0
A1		200901	200902	3	0
A1		200905	200902	0	0
B1		200905	200908	0	0
B1 		200906	200908	1	0
B1		200907	200908	2	0
B1		200910	200908	0	0
A1 		200902	200902	3	1
A1		200903	200902	4	2
A1 		200904	200902	4	3
A1		200906	200902	1	4
A1		200907	200902	2	5
B1		200908	200908	2	1
B1		200909	200908	2	2
B1		200911	200908	1	3
B1		200912	200908	1	4


Try!
 
Share this answer
 
Comments
kirthiga S 16-Jun-16 1:04am    
User Month Lmonth ARR RN
A1 200906 200902 1 4
A1 200907 200902 2 5

Thank you.
For above rows RN updated as 4 & 5. I need RN as 1 & 2.
Maciej Los 16-Jun-16 1:41am    
Why? Does these rows not meet criteria?
kirthiga S 16-Jun-16 1:44am    
If ARR=0 RN becomes 0.When ARR again starts RN has to start again from 1
Maciej Los 16-Jun-16 1:48am    
So, if between months 200904 and 200906 there's a 200905 with ARR = 0 then RN have to be re-calculated. Am i right?
kirthiga S 16-Jun-16 1:50am    
Yes Right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900