SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 enumerating row number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

slimt_slimt
Aged Yak Warrior

Switzerland
746 Posts

Posted - 01/10/2013 :  07:46:17  Show Profile  Reply with Quote
Hi,

I have two columns c1 and c2:

c1 c2
1 1
2 0
3 0
4 1
5 1
6 0
7 0
8 0
9 0
10 1
11 0
12 0

what i want is to get column c3 which will enumerate each part of "pairs" between 1 and next 1.
so result for C3:


c1 c2 c3
1 1 1
2 0 2
3 0 3
4 1 1
5 1 1
6 0 2
7 0 3
8 0 4
9 0 5
10 1 1
11 0 2
12 0 3

thank you

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 01/10/2013 :  09:56:22  Show Profile  Reply with Quote
See if this will work for you
SELECT
	t.c1,t.c2,t.c1-(t2.n-1) AS c3
FROM
	Tbl t
	CROSS APPLY
	( 
		SELECT MAX(c1) AS n FROM Tbl t2
		WHERE t2.c1 <= t.c1 AND t2.c2 = 1
	) t2;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000