Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 20th, 2007, 04:22 PM
Registered User
Join Date: Jul 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL help needed

Hi pals,

I need small help.
I have a table with data as follows.

select * from test_data

c1 c2 c3
111 4 101
122 3 101
133 2 102
144 2 103
155 1 103

Now i need to generate a new column c4 as sequence number in the output

I need to get the output something as follows

c4 c1 c2 c3
1 111 4 101
2 122 3 101
1 133 2 102
1 144 2 103
2 155 1 103

The newly generated column contains sequence numbers starting from 1 and the sequence should be resetted again to 1 whenever a new c3 value is encountered(for example when the c3 value changes from 101 to 102 the sequence should be resetted to 1).

Can anyone help me out on this regard.

Thanks in advance,

Reply With Quote
  #2 (permalink)  
Old November 21st, 2007, 11:22 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

As this isn't really a normal set type operation I don't believe there is a straightforward operation you can add to a standard select query to do it.

I think you'll need to write some SQL code using a cursor and test the values as you spin thru the rows of the initial set. Probably best to put this in a stored procedure.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
asp.net / sql help needed keithroby ASP.NET 3.5 Basics 16 November 14th, 2008 04:29 PM
HELP NEEDED: Trouble Connecting with SQL Server mutinousguy ASP.NET 2.0 Basics 2 September 17th, 2007 01:21 PM
Good sql interview questions are needed. BigDude SQL Server 2000 1 August 30th, 2005 09:15 AM
SQL Select problem (subquery needed??) deian SQL Language 7 February 3rd, 2004 02:40 PM
SQL syntax for my query - guru needed badgolfer Access 2 September 20th, 2003 01:52 PM

All times are GMT -4. The time now is 12:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.