Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2007, 04:22 PM
Registered User
 
Join Date: Jul 2007
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,
franky


 
Old November 21st, 2007, 11:22 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.

-Peter





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





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