Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Create new column into table from given table.

Author  Topic 

tamancha.1
Starting Member

37 Posts

Posted - 2010-04-21 : 17:02:01
Here is my table:

user_id time_stamp
38441030 3/23/2010 14:06:34
38441030 3/23/2010 14:06:34
43477060 3/29/2010 18:03:20
43477060 3/29/2010 18:03:21
58635220 3/5/2010 13:11:43
58635220 3/5/2010 13:11:43
58635220 3/5/2010 13:40:36
58635220 3/5/2010 13:41:18
58635220 3/5/2010 13:42:58



I want to create a new table, ordering each user_id by time and append a count column in front of it:

user_id time_stamp imp_numb
38441030 3/23/2010 14:06:34 1
38441030 3/23/2010 14:06:34 2
43477060 3/29/2010 18:03:20 1
43477060 3/29/2010 18:03:21 2
58635220 3/5/2010 13:11:43 1
58635220 3/5/2010 13:11:43 2
58635220 3/5/2010 13:40:36 3
58635220 3/5/2010 13:41:18 4
58635220 3/5/2010 13:42:58 5

Please help.
Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-21 : 17:56:59
Have you looked at the ROW_NUMBER() function?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-04-21 : 17:59:10
You don't need a new table just for this sequence. You can generate it in a SELECT like below.

SELECT user_id,time_stamp, row_number () over (partition by user_id order by time_stamp)
from table
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-04-21 : 17:59:39
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-22 : 03:15:27
See here how you can effectively make use of row_numbeR() function for various purposes
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -