473,545 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ 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 6511
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
5103
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 this transcation. Why DB2 does not release the X lock after a failed insert??? We use DB2 UDB EEE Version 7.2 Fixpak 9, but we also can reproduce...
3
4579
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 field columns. The first 4 columns are descriptor fields. The 5th column is a value. You know there are duplicate rows when the contents of the...
1
1775
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
379
by: vijayk | last post by:
Hi, Have a table like Pkey SNO ------------------- AA 0 BB 0 CC 0 AA 0 BB 0
6
13750
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 t1 ON relevant_stuff WHERE (lots of conditions) After re-reading the relevant pat ofVol 1 of the SQL Reference I am unablee to see how this is...
7
9768
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 I've got stuck trying to write a DELETE statement. Here's the table I'm working on: CREATE TABLE `articles_categories` (
5
8408
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: -------------------------------- -- Eliminating Duplicate rows -- -------------------------------- -- select all into a temp table. -- truncate origional...
4
5430
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 the number of duplicate rows based on a particular cell value of each these duplicate rows. I mean all the cell values of a row will not be...
7
1691
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 count(*) > 1"); $count = mysql_num_rows($duplicates); if ($count > 0) { while ($row = mysql_fetch_assoc($duplicates)) { $bid_price = $row; } echo...
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3490
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.