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
 Counting rows one by one
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

munya
Starting Member

Rwanda
6 Posts

Posted - 07/25/2012 :  09:00:58  Show Profile  Reply with Quote
Hello there,


Need some help on counting rows one by one in a table of
some identical values:
I want to update No_APPEARANCE on each row in a table
depending on IDno.
Example My table looks like this:

IDno NO_APPEARANCE
1 1
1 2
1 3
1 4
2 1
2 2
3 1
3 2
4
4
4
4
5
5
5
6
6
7
8
8
3

Please I need yr support

webfred
Flowing Fount of Yak Knowledge

Germany
8529 Posts

Posted - 07/25/2012 :  09:09:57  Show Profile  Visit webfred's Homepage  Reply with Quote
This?
declare @Sample table (id int, NoOcc int)
insert @Sample(id)
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 3 union all
select 3

select * from @Sample

update dt
set NoOcc=dt.rownum
from
(select id, NoOcc, row_number() over (partition by id order by id) as rownum from @Sample)dt

select * from @Sample




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

munya
Starting Member

Rwanda
6 Posts

Posted - 07/25/2012 :  09:29:05  Show Profile  Reply with Quote
Oh Man It worked thanks a lot. Can you please explain a bit to me how it works? Or have a link to read more on this concept?
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8529 Posts

Posted - 07/25/2012 :  09:40:26  Show Profile  Visit webfred's Homepage  Reply with Quote
What in special do you want to know?
The use of a "derived table"?
The use of "row_number()"?
The use of alias names for columns and tables?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 07/25/2012 :  09:55:27  Show Profile  Visit SwePeso's Homepage  Reply with Quote
I'd say "Books Online".



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48115 Posts

Posted - 07/25/2012 :  10:19:00  Show Profile  Reply with Quote
quote:
Originally posted by munya

Oh Man It worked thanks a lot. Can you please explain a bit to me how it works? Or have a link to read more on this concept?



http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.08 seconds. Powered By: Snitz Forums 2000