Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

sql - DB2 Copy one column values to another column

In DB2, I have 2 tables - TableA and TableB. In Table A, there are 10 rows. In Table B, there are 12 rows. Table A has a column named IntValueA with Integer type. Table B has a column named IntValueB with Integer type.

I want TableA's IntValueA to replace any 10 TableB's IntValueB values, such that, TableA and TableB will have 10 common values where IntValueA=IntValueB.

IntValueA can replace ANY of IntValueB. No condition.

Can someone please help me how the DB2 query should look like?

question from:https://stackoverflow.com/questions/66054296/db2-copy-one-column-values-to-another-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this.

UPDATE 
(SELECT IntValueB, ROWNUMBER() OVER() RN_ FROM TableB) B
SET IntValueB = 
(
SELECT IntValueA 
FROM (SELECT IntValueA, ROWNUMBER() OVER() RN_ FROM TableA) A
WHERE A.RN_ = B.RN_
)
WHERE RN_ <= 10;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...