简体   繁体   中英

SQL to find the matching row between two tables of same schema

I have two tables, say X and X_STAGING.

They are exactly identical in columns ie schema is same. However, the number of rows are different. I know that the first row of X is there in X_STAGING - the data was partially copied over from X_STAGING to X. However I need to know exactly which row of the X_STAGING contains the data, that went into the first row of X.

At the moment I am using this

SELECT 
SUM(MATCH) 
FROM 
(
SELECT 
CASE WHEN X_STAGING.KEY_ID='KEY_FROM_THE_FIRST_ROW_OF_X' THEN 1 ELSE 0 END AS MATCH 
FROM 
X_STAGING 
WHERE ROWNUM<2550000
) 

Changing the ROWNUM I can find out at which ROWNUM does the count get to 1. And then my adjusting ROWNUM I can eventually get to the particular row.

This will work, but I am sure there has to be a quicker and more clever way of doing this. Please help.

Note: I am working on Linux, DB2 environment.

I don't understand what you are trying to accomplish, but the following does what you are asking for:

SELECT 
MAX(MATCH) 
FROM 
(
SELECT 
CASE WHEN X_STAGING.KEY_ID='KEY_FROM_THE_FIRST_ROW_OF_X' THEN ROWNUM ELSE 0 END AS MATCH 
FROM 
X_STAGING 
) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM