Solved

SQL update question

Posted on 2007-11-26
4
221 Views
Last Modified: 2010-04-21
I have a table with 2 columns. Column1 is given and sorted ascending, what i need to do is update column2 with corresponding values.  Column 2 just should increment by one and reset to one when the value in column1 changes.



Col1      Col2
1      1
1      2
1      3
1      4
1      5
2      1
2      2
2      3
2      4
3      1
3      2
3      3
3      4
3      5
3      6
3      7
3      8
3      9
3      10


Thanks
sopheak
0
Comment
Question by:sopheak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Yveau
ID: 20353479
Here goes:

Hope this helps ...

-- creating original situation
create table #Y(Col1 int,Col2 int)
go
 
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(2, NULL)
insert into #Y values(2, NULL)
insert into #Y values(2, NULL)
insert into #Y values(2, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
go
 
-- actual code starts here
alter table #Y
add col3 int identity(1,1)
go
 
declare @c1 int
declare @c1p int
declare @c3 int
declare @c2 int
 
declare Yveau cursor
for
select col1, col3
from  #Y
 
open yveau
fetch yveau
into @c1, @c3
 
while @@fetch_status = 0
begin
        if isnull(@c1p,-1) != @c1
        begin
                set @c2 = 1
        end
        else
        begin
                set @c2 = @c2 + 1
        end
 
        update #y
        set    col2 = @c2
        where  col3 = @c3
 
        set @c1p = @c1
 
        fetch yveau
        into @c1, @c3
end
 
close yveau
deallocate yveau
 
alter table #Y
drop column col3
 
select * from #Y
 
-->> result:
Col1        Col2
----------- -----------
1           1
1           2
1           3
1           4
1           5
2           1
2           2
2           3
2           4
3           1
3           2
3           3
3           4
3           5
3           6
3           7
3           8
3           9
3           10
 
--

Open in new window

0
 
LVL 18

Expert Comment

by:Yveau
ID: 20353492
To be completely perfect, the cursor should have been declared like this:

Hope this helps ...

declare Yveau cursor
for
select col1, col3 
from #Y 
order by col3 asc
 
--

Open in new window

0
 
LVL 35

Accepted Solution

by:
David Todd earned 180 total points
ID: 20354290
Hi,

Avoid cursors like the plauge ...

If you are using SQL 2005, look at the row_number() function.

If you are using SQl 2000 or earlier, then I'd use a temp table with an identity to uniquely identify those rows and then build an update statement, rather than using a loop or cursor.

HTH
  David
use tempdb
go
 
 
if object_id( N'tempdb..#Y', N'U' ) is not null 
	drop table #Y;
	
create table #Y(Col1 int,Col2 int)
go
 
set nocount on
 
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(1, NULL)
insert into #Y values(2, NULL)
insert into #Y values(2, NULL)
insert into #Y values(2, NULL)
insert into #Y values(2, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
insert into #Y values(3, NULL)
 
set nocount off
 
select *
from #Y
go
 
select 
	row_number() over( partition by col1 order by col1 ) as RowNumber
	, col1
from #Y
go
 
update y
	set y.col2 = y.RowNumber
from 
	(
	select 
		row_number() over( partition by col1 order by col1 ) as RowNumber
		, col1
		, col2
	from #Y	
	) y
 
select *
from #Y
go

Open in new window

0
 

Author Closing Comment

by:sopheak
ID: 31411075
Thanks for the row_number
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL to Update Table Dynamically 2 64
Error when saving to sql table a '/' 5 32
create insert script based on records in a table 4 28
Help Required 2 46
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question