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
 Need Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yaman
Posting Yak Master

India
213 Posts

Posted - 05/04/2012 :  08:48:00  Show Profile  Reply with Quote
Sir ,

I have one table

======================
name age count
======================
mukesh 23 NULL
mukesh 23 NULL
mukesh 23 NULL
mukesh 23 NULL
yaman 21 NULL
yaman 21 NULL
yaman 21 NULL

I need result like

======================
name age count
======================
mukesh 23 0
mukesh 23 1
mukesh 23 2
mukesh 23 3
yaman 21 0
yaman 21 1
yaman 21 2
==========================


Please help me out Sir


Yaman

jimf
Flowing Fount of Yak Knowledge

USA
2868 Posts

Posted - 05/04/2012 :  09:05:39  Show Profile  Reply with Quote
select name,Age, ROW_NUMBER() over(partition by name order by age) -1
from yourtable


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

yaman
Posting Yak Master

India
213 Posts

Posted - 05/04/2012 :  10:24:19  Show Profile  Reply with Quote
quote:
Originally posted by jimf

select name,Age, ROW_NUMBER() over(partition by name order by age) -1
from yourtable


Jim

Everyday I learn something that somebody else already knew



Thanks Sir,

I I need to update table rows in one column then how can I update rows with that values in one query. Please help

Yaman
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/04/2012 :  10:38:21  Show Profile  Reply with Quote
You can put Jim's query into a subquery or cte and then do the update. First run this query to make sure that you are getting the results you want.
;with cte as
(
	select name,Age, [count],ROW_NUMBER() over(partition by name order by age) -1 AS newCount
	from yourtable
)
SELECT * FROM cte;
If the newCount column looks like it has the correct values, then run this query to do the update:
;with cte as
(
	select name,Age, [count],ROW_NUMBER() over(partition by name order by age) -1 AS newCount
	from yourtable
)
UPDATE cte SET [count] = newCount;
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