473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Update Duplicate rows

Hi

I have a table like the one below

Pkey Sno
-----------------
AA 0
BB 0
CC 0
AA 0
BB 0
BB 0

and I want to update the duplicate rows like this

Pkey Sno
-----------------
AA 1
BB 1
CC 0
AA 2
BB 2
BB 3

Please suggest a way...

thanks

Jan 17 '06 #1
9 6482
vijayk wrote:
Hi

I have a table like the one below

Pkey Sno
-----------------
AA 0
BB 0
CC 0
AA 0
BB 0
BB 0

and I want to update the duplicate rows like this

Pkey Sno
-----------------
AA 1
BB 1
CC 0
AA 2
BB 2
BB 3

Please suggest a way...


UPDATE t AS o
SET sno = ROWNUMBER() OVER ( PARTITION BY pkey )
WHERE ( SELECT COUNT(*)
FROM t AS i
WHERE o.a = i.a ) > 0

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jan 17 '06 #2
Hi
I am getting

Pkey Sno
-----------------
AA 1
BB 1
CC 1 --instead of 0
AA 2
BB 2
BB 3

Jan 17 '06 #3
vijayk wrote:
Hi
I am getting

Pkey Sno
-----------------
AA 1
BB 1
CC 1 --instead of 0
AA 2
BB 2
BB 3


Sorry... I copied the wrong query. Make the comparison in the WHERE clause
a "WHERE ( SELECT ... ) > 1".

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jan 17 '06 #4
Knut Stolze wrote:
vijayk wrote:
Hi
I am getting

Pkey Sno
-----------------
AA 1
BB 1
CC 1 --instead of 0
AA 2
BB 2
BB 3


Sorry... I copied the wrong query. Make the comparison in the WHERE
clause a "WHERE ( SELECT ... ) > 1".


p.s: I hope you do understand what this statement is doing. Because your
question indicates that you just tried it as I posted it.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jan 17 '06 #5
Given your table is t(pkey, sno):

UPDATE
(SELECT sno, new_sno FROM (
SELECT sno,
ROW_NUMBER() OVER(PARTITION BY pkey) new_sno,
COUNT(*) OVER(PARTITION BY pkey) cnt
FROM t
) a WHERE cnt > 1
) b
SET b.sno = b.new_sno
-Eugene

Jan 17 '06 #6
Eugene F wrote:
Given your table is t(pkey, sno):

UPDATE
(SELECT sno, new_sno FROM (
SELECT sno,
ROW_NUMBER() OVER(PARTITION BY pkey) new_sno,
COUNT(*) OVER(PARTITION BY pkey) cnt
FROM t
) a WHERE cnt > 1
) b
SET b.sno = b.new_sno

:-)
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 17 '06 #7
Serge, are you smiling having found my solution funny or may be silly?
But I actually tested it worked.
Oh, I see ... LUW vs. iSeries from an earlier topic makes you
smiling... right?

:-)

-Eugene

Jan 17 '06 #8
Eugene F wrote:
Serge, are you smiling having found my solution funny or may be silly?
But I actually tested it worked.
Oh, I see ... LUW vs. iSeries from an earlier topic makes you
smiling... right?

:-)

I had a ton of folks drafted to get this and the other SQL features
added in DB2 V8.1 FP4 for LUW working in time for the TPC-C effort.
Posts like this one help validate my belief that what was added was all
good stuff, well worth the investment and that TPC-C is more
representative than many people give it cridit.

Call me a proud father if you wish :-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 18 '06 #9
You got it!
:-))

-Eugene

Jan 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within...
3
by: dan graziano | last post by:
Hi, How do you suggest is the best way to check for duplicate rows in an access table. And once one knows if there are duplicates, to remove all but one. In my access table, there are 5...
1
by: Asha | last post by:
greetings, does anyone have any idea how to delete duplicate rows in a dataset gracefully (the fast and easy way)
0
by: vijayk | last post by:
Hi, Have a table like Pkey SNO ------------------- AA 0 BB 0 CC 0 AA 0 BB 0
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
7
by: Jon Maz | last post by:
Hi, I have a MySql problem I hope someone can help me with. I'm trying to run an update on a linking table, the update is running into a Primary Key constraint violation, and in my workaround...
5
jamesd0142
by: jamesd0142 | last post by:
My manager and I where looking at some complex code to eliminate duplicate records in a database table. then it hit me how its done easily... so i thought i'd share it... In English:...
4
by: ravir81 | last post by:
Hi, I am currently working on excel validation using Perl. I am new to Excel validation but not for Perl. I have a question regarding one of the validation. Could anyone please tell me how to get...
7
by: canabatz | last post by:
i got this query that finds me the duplicate rows $duplicates = mysql_query("SELECT bid_price, count(*) FROM bidding_details where bid_id= '1128' and sortbid = '1' GROUP BY bid_price having...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.