Post by SphenixPost by Serge RielauDB2 Version 5? I hope you are talking about DB2 V5R? for iSeries.
You want to prepend a '?' to all IDs for which there is NO match in B?
Is B.DUP_ID nullable?
UPDATE TABLEA A
SET A.ID = '?' || A.ID
WHERE NOT EXISTS(SELECT 1 FROM TABLEB B WHERE A.INDEX = B.INDEX)
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
IOD Conference
http://www.ibm.com/software/data/ondemandbusiness/conf2006/
above statement can be use, however my friend said not recommended to
update a table where have another select statement
e.g.
UPDATE TABLEA A
SET A.ID = '?' || A.ID
WHERE NOT EXISTS(SELECT 1 FROM TABLEB B WHERE A.INDEX = B.INDEX)
or
UPDATE TABLEA A
SET A.ID = '?' || A.ID
WHERE A.ID NOT IN (SELECT B.ID FROM TABLEB B WHERE A.INDEX =
B.INDEX)
-----------------
TABLEA
------------------
INDEX ID
1 A
2 B
3 B --Duplicated Record
4 D
5 E
6 E --Duplicated Record
7 G
8 H
9 J
10 J --Duplicated Record
------------------
so my task is update duplicated record of ID with '?' on the biggest
number of INDEX
so my result should be INDEX 3, 6 & 10 with '?' infront the ID
That is definitely NOT what that SQL Server query you posted is doing.
Aside where is TABLEB? You only posted half the data!
Here is what does the job given the TABLEA you posted:
CREATE TABLE TABLEA(INDEX INT, ID VARCHAR(10));
INSERT INTO TABLEA VALUES
(1 , 'A'),
(2 , 'B'),
(3 , 'B'),
(4 , 'D'),
(5 , 'E'),
(6 , 'E'),
(7 , 'G'),
(8 , 'H'),
(9 , 'J'),
(10, 'J');
UPDATE TABLEA A
SET ID = '?' || ID
WHERE EXISTS(SELECT 1 FROM TABLEA B
WHERE A.ID = B.ID
AND A.INDEX > B.INDEX);
SELECT * FROM TABLEA;
INDEX ID
----------- ----------
1 A
2 B
3 ?B
4 D
5 E
6 ?E
7 G
8 H
9 J
10 ?J
10 record(s) selected.
In DB2 V8.2 for LUW (I still don't know which platform you are using)
you can write this, which is much faster:
UPDATE
(SELECT ID,
ROWNUMBER() OVER(PARTITION BY ID ORDER BY INDEX) AS rn
FROM TABLEA) AS U
SET ID = '?' || ID
WHERE rn > 1;
Cheers
Serge
PS: I never ask my friends for SQL advise ;-)
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
IOD Conference
http://www.ibm.com/software/data/ondemandbusiness/conf2006/