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
 Not update the duplicate type
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Flaterik
Starting Member

Italy
26 Posts

Posted - 09/25/2012 :  09:58:55  Show Profile  Reply with Quote
Hi people i have a table like this

COD TYPE VALUE DEFAULT
C001 WWW XXXX 0
C001 EMAIL YYY 0
C001 EMAIL ZZZ 0
C001 CEL NNN 0
C002 WWW XXXX 0

etc etc

So i must update the filed 0 with 1
But because of a key value, i can update from 0 to 1 the same field of the same customer
So i mus put 1 only on C001 EMAIL YYY , not in C001 EMAIL ZZZ.
HOw i can remove the duplicated type grouped by COD? I can order this with another field


************************************************
the world is strange but people are crazy

Edited by - Flaterik on 09/25/2012 10:02:31

Ifor
Aged Yak Warrior

590 Posts

Posted - 09/25/2012 :  10:23:37  Show Profile  Reply with Quote

-- *** Test Data ***
CREATE TABLE #t
(
	COD char(4) NOT NULL
	,[Type] varchar(5) NOT NULL
	,Value varchar(5) NOT NULL
	,[Default] int NOT NULL
);
INSERT INTO #t
SELECT 'C001', 'WWW', 'XXXX', 0 
UNION ALL SELECT 'C001', 'EMAIL', 'YYY', 0
UNION ALL SELECT 'C001', 'EMAIL', 'ZZZ', 0
UNION ALL SELECT 'C001', 'CEL', 'NNN', 0
UNION ALL SELECT 'C002', 'WWW', 'XXXX', 0;
-- *** End Test Data ***

select * from #t;

WITH CODTypeOrder
AS
(
	SELECT *
		,ROW_NUMBER() OVER (PARTITION BY COD, [Type] ORDER BY Value) rn
	FROM #t
)
UPDATE CODTypeOrder
SET [Default] = 1
WHERE rn = 1;

select * from #t;
Go to Top of Page

Flaterik
Starting Member

Italy
26 Posts

Posted - 09/25/2012 :  11:17:21  Show Profile  Reply with Quote
Hi, thank you very much for the response.

It's right what you say to me but ther's another problem

I must update a table of my db called PeopleAddressMedia.
How i can do this? In your solution you create a CTE and update this

I must update the other table
Thanks

************************************************
the world is strange but people are crazy
Go to Top of Page

Ifor
Aged Yak Warrior

590 Posts

Posted - 09/25/2012 :  11:52:53  Show Profile  Reply with Quote

WITH CODTypeOrder
AS
(
	SELECT *
		,ROW_NUMBER() OVER (PARTITION BY COD, [Type] ORDER BY Value) rn
	FROM PeopleAddressMedia
)
UPDATE CODTypeOrder
SET [Default] = 1
WHERE rn = 1;
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.06 seconds. Powered By: Snitz Forums 2000