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
 Update column with duplicates to unique values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Jaxon
Starting Member

5 Posts

Posted - 05/15/2012 :  00:01:30  Show Profile  Reply with Quote
I need to update a column that has duplicate values in it and rename them so they have a sequential number after it. My table has about 2000 rows of data. So this is what I currently have.

ID Type Size
Valve1 Valve 100
Valve2 Valve 150
Manhole Manhole 1050
Manhole Manhole 1050
Cesspit1 Cesspit 200
Cesspit2 Cesspit 200

and this is what I would like.

ID Type Size
Valve1 Valve 100
Valve2 Valve 150
Manhole1 Manhole 1050
Manhole2 Manhole 1050
Cesspit1 Cesspit 200
Cesspit2 Cesspit 200

I have about 1800 Manholes which I want to number from Manhole1 to Manhole1800. This is just a sample of the data.

I don't mind creating a new ID column if need be. I hope this is clear?

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/15/2012 :  00:05:58  Show Profile  Reply with Quote
have a look at ROW_NUMBER() function in t-sql

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/15/2012 :  08:48:52  Show Profile  Reply with Quote
Also, your naming convention is terribad. Instead of an ID of Manhole1, it should just be a number. The TYPE column tells you what you have.
Another helpful link: http://en.wikipedia.org/wiki/Database_normalization









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 05/15/2012 :  08:59:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DECLARE	@Sample TABLE
	(
		ID VARCHAR(10),
		[Type] VARCHAR(10),
		Size INT
	)

INSERT	@Sample
VALUES	('Valve1', 'Valve',     100),
	('Valve2', 'Valve',     150),
	('Manhole', 'Manhole', 1050),
	('Manhole', 'Manhole', 1050),
	('Cesspit1', 'Cesspit', 200),
	('Cesspit2', 'Cesspit', 200)

SELECT * FROM @Sample

-- SwePeso
;WITH cteTarget(ID, SeqID, Items)
AS (
	SELECT	ID,
		ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Size) AS SeqID,
		COUNT(*) OVER (PARTITION BY ID) AS Items
	FROM	@Sample
)
UPDATE	cteTarget
SET	ID = ID + CAST(SeqID AS VARCHAR(12))
WHERE	Items > 1

SELECT * FROM @Sample



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Jaxon
Starting Member

5 Posts

Posted - 05/15/2012 :  20:27:41  Show Profile  Reply with Quote
Awesome thanks so much SwePeso, worked like a charm.

@DonAtWork thanks for the advice, that was just a sample

@visakh16 thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/15/2012 :  21:56:16  Show Profile  Reply with Quote
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.05 seconds. Powered By: Snitz Forums 2000