How to find difference in two tables
Hi all,
I am having 2 tables (Table1 and Table2 ) same structure.
1. Table1 is having some data.
2. Now, i got data in CSV file, which was loaded into Table2.
This data contains....
A. Data of Table1
B. Some colum values are different compare to Table1 column values.
(Pkey is same, but column values are different in Table1 and Table2)
C. Additional rows are there compared to Table1.
Now, what i want is......
I want to select the data from Table2,
1. The rows in which column values are different from the Table1
2. New records added from CSV file (i.e. Those records that are not
there in Table1).
Thanks in advance,
Pal
Thanks for your solution. It is working, but my problem is
Table1
COL1 COL2 COL3 COL4
1 col2 col3 col4
2 col2 col3 col4
3 col2 col3 col4
4 col2 col3 col4
Table2
COL1 COL2 COL3 COL4
5 col2 col3 col4 -- New row
6 col2 col3 col4 -- New row
1 col2 col3 col44 -- Different column value
2 col2 col3 col4
3 col2 col33 col4 -- Different column value
4 col2 col3 col4
select * from test2
minus
select * from test1
COL1 COL2 COL3 COL4
1 col2 col3 col44 --- needs to be update
3 col2 col33 col4 --- needs to be update
5 col2 col3 col4 --- needs to be insert
6 col2 col3 col4 --- needs to be insert
Actually, I need to update and insert those records through LIVE UPDATE in the remote database. In this case, Table1 data is in the remote database. The output of MINUS statement needs to be implimented in Table1, which is in remote database.
Any suggestion will be appreciated. Thanks,
Pal
Similar Messages
-
Find Differences between two tables at column level
Hi,
I have 2 tables one live table and the other History table..
If i have to find differences between live table and the latest version in the History table and also find which column got chaanged
How would i do that for a table which has many columns and i need each column for which the value has changed for a id
for ex:
Table 1 (LIve) Table 2 (History)
ID col1 col2 Version ID col1 col2 Version
1 ABC 123 V1 1 ABCD 123 v2
2 NBS 1234 V1 2 NBS 123 V2
Result set should be
Result Set:
ID col which changed
1 col1
2 col2
Because the values for that column had been changed
Except gives me all the differences not just the column level ..The dynamic version using schema views... :D
--Build a coulple OF testing tables to play with
CREATE TABLE dbo.Table1 (
ID INT IDENTITY(1,1) PRIMARY KEY,
Col1 INT,
Col2 INT,
Col3 INT
CREATE TABLE dbo.Table2 (
ID INT IDENTITY(1,1) PRIMARY KEY,
Col1 INT,
Col2 INT,
Col3 INT
INSERT dbo.Table1 (Col1,Col2,Col3) VALUES
(123,456,789),
(111,222,333),
(444,555,666),
(777,888,999),
(321,345,769),
(179,753,758),
(362,362,236),
(856,874,896),
(821,729,324)
INSERT dbo.Table2 (Col1,Col2,Col3) VALUES
(123,456,789),
(111,999,333), --col2 diff
(444,555,666),
(777,888,999),
(321,345,123), --col3 diff
(179,753,758),
(362,362,236),
(234,874,896), --col1 diff
(821,729,324)
And then the actual solution...
DECLARE
@t1 VARCHAR(10) = 'Table1',
@t2 VARCHAR(10) = 'Table2'
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
SELECT
c.TABLE_SCHEMA,
c.TABLE_NAME,
c.COLUMN_NAME,
c.ORDINAL_POSITION,
CASE WHEN u.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS PK
INTO #temp
FROM
INFORMATION_SCHEMA.COLUMNS c
JOIN INFORMATION_SCHEMA.TABLES t
ON c.TABLE_NAME = t.TABLE_NAME
AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
ON c.COLUMN_NAME = u.COLUMN_NAME
AND c.TABLE_NAME = u.TABLE_NAME
AND c.TABLE_SCHEMA = u.TABLE_SCHEMA
AND OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
WHERE 1 = 1
AND t.TABLE_TYPE = 'BASE TABLE'
AND c.TABLE_NAME IN (@t1,@t2)
ORDER BY
c.TABLE_SCHEMA, c.TABLE_NAME, c.ORDINAL_POSITION
DECLARE @select VARCHAR(MAX)
SELECT @select = COALESCE(@select + ', ', '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '.' + t.COLUMN_NAME
FROM #temp AS t
ORDER BY t.TABLE_NAME, t.ORDINAL_POSITION
DECLARE @from VARCHAR(MAX)
SELECT @from = COALESCE(@from + ' FULL JOIN ', '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME
FROM #temp AS t
WHERE t.PK = 1
ORDER BY t.TABLE_NAME
DECLARE @on VARCHAR(MAX)
SELECT @on = COALESCE(@on + ' = ', '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '.' + t.COLUMN_NAME
FROM #temp AS t
WHERE t.PK = 1
ORDER BY t.TABLE_NAME, t.ORDINAL_POSITION
DECLARE @where VARCHAR(MAX)
SELECT @where = COALESCE(@where + CASE WHEN t.TABLE_NAME = @t1 THEN ' OR ' ELSE ' <> ' END, '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '.' + t.COLUMN_NAME
FROM #temp AS t
WHERE t.PK = 0
ORDER BY t.ORDINAL_POSITION, t.TABLE_NAME
DECLARE @sql VARCHAR(MAX) = '
SELECT ' + @select + '
FROM ' + @from + '
ON ' + @on + '
WHERE ' + @where
EXEC (@sql)
HTH,
Jason -
Oracle 11g sql to find difference from two table data
I have two tables ACTUAL AND ESTIMATE having unique column(sal_id, gal_id) and amount, tax, date columns.
ACTUAL table
actual_id, sal_id, gal_id, process_flag, amount, tax date
A1 101 201 Y 10 1 27-Aug-12
A2 102 202 Y 20 2 27-Aug-12
A3 102 202 N 30 3 29-Aug-12
A4 302 402 N 40 3 30-Aug-12
ESTIMATE table
estimate_id, sal_id, gal_id, process_flag, amount, tax date
E1 301 401 Y 5 1 19-Aug-12
E2 302 402 Y 45 4 20-Aug-12
E3 302 402 Y 50 5 25-Aug-12
E4 301 403 Y 10 2 27-Aug-12
E5 301 403 N 15 3 28-Aug-12
E6 101 201 Y 12 3 29-Aug-12
Now My FINAL table, 1) Should have record for unprocessed (process_flag='N') from ACTUAL/ESTIMATE table for difference in amount and tax for (sal_id + gal_id) combination. If more than one processed record there, then max (date) should be consider.
FINAL table
actual_id, estimate_id, sal_id, gal_id, amount, tax
A3 null 102 202 10 1 (A3-A2 actual-actual)
A4 null 302 402 -10 -2 (A4-E3 actual-estimate with max date)
null E5 301 403 5 1 (E5-E4 estimate-estimate)
null E6 101 201 2 2 (E6-A1 estimate-actual)
So basically I need a query for (A-A) U (B-B) U (A-B) U (B-A).
I am using Oracle 11g Please help me.
Edited by: Suresh on Aug 31, 2012 7:38 PM
Edited by: Suresh on Aug 31, 2012 7:38 PM
Edited by: Suresh on Aug 31, 2012 10:53 PMYou need to provide better explanation here.A-A is always null.
You might be looking for something like this..
Untested version and also i cannot read your mind
select actual_id, sal_id, gal_id, process_flag,ld_amount,ld_tax,e_date
from(
select actual_id, sal_id, gal_id, process_flag,e_date, amount-lead(amount) over(partition by sal_id, gal_id order by e_date desc) ld_amount,
tax-lead(tax) over(partition by sal_id, gal_id order by e_date desc) ld_tax,
rank() over(partition by sal_id, gal_id order by e_date desc)rnk
from
(select * from actual a union
select * from estmt e)
) where rnk=1 -
How to find differences between two columns
Hello I need the formula to spot differences between two columns and to write on a third one if the same name appear on the two columns.
For example:
rome berlin true
berlin moscow false
chicago toronto true
toronto chicago true
florence
Thanks
LorenzoYou should be able to use COUNTIF where the range is th first column and the condition is the word in the second column. If the result is greater than zero then the word is in both columns.
-
How to find the structural difference between two tables
Hi all,
How to find the structural difference between two tables .
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
Thanks,
P Prakashyou could try something similar to this, for each table pair that you want to compare:
SELECT 'TABLE_A has these columns that are not in TABLE_B', DIFF.*
FROM (
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_A'
MINUS
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_B'
) DIFF
UNION
SELECT 'TABLE_B has these columns that are not in TABLE_A', DIFF.*
FROM (
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_B'
MINUS
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_A'
) DIFF;that's assuming, column_name, data_type and data_length are all you want to compare on. -
Need to find the Difference between two table
Hello ,
I have stucked in program as below scenario:-
I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
Example TableA
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
80 DEF 6005 YY 446 YY8
TableB
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
81 DEF 6005 Yu 447 YY8
I need to the out put like this :-
The Diffence between two table is
TableA.COL1=80 TableB.Col1=81, Different
TableA.Col4=YY TableB.col4=Yu,Different
TableA.Col5=446TableB.col5=447,Different
Please suggest me to write the pl/sql program for the same
thanx in advance
KKThanx friends for all your efforts
I have a sample code for the same,this will compare the two tables for single row in each table .
what r the modification needed for the multiple rows of values in the two tables??
Please suggest!!
CREATE OR REPLACE PROCEDURE test_compare
IS
TYPE t_col
IS
TABLE OF VARCHAR2 (30)
INDEX BY PLS_INTEGER;
l_col t_col;
j NUMBER := 0;
l_sql VARCHAR2 (2000);
col1 VARCHAR2 (30);
col2 VARCHAR2 (30);
val1 NUMBER;
val2 NUMBER;
status VARCHAR2 (30);
CURSOR c1
IS
SELECT column_id, column_name
FROM all_tab_columns
WHERE table_name = 'TEST1';
BEGIN
FOR i IN c1
LOOP
j := j + 1;
l_col (j) := i.column_name;
END LOOP;
FOR k IN 1 .. j
LOOP
l_sql :=
'SELECT '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST2.'
|| l_col (k)
|| ', '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST1.'
|| l_col (k )
|| ', '
|| 'DECODE(TEST2.'
|| l_col (k)
|| ' -TEST1.'
|| l_col (k)
|| ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
IF status = 'CHANGED'
THEN
DBMS_OUTPUT.put_line( 'TEST2.'
|| col1
|| '='
|| val1
|| ', TEST1.'
|| col2
|| '='
|| val2
|| ', '
|| status);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
END;
/ -
How to find the list of Tables....
Hi
How to find the list of Tables associated with When a Goods Issue is done for a Process Order ??
Kindly tell me step-by-step procedure in browsing it.
cheers
MaruthiRamHi
goto SE16 Click F4, You have two options Information system & Sap Applications.
Click on SAP Applications & selec the application you wnat to see, for E.g Purchasing you can click on materials managemnt, Purchasing if you drill down you will see the list of tables....
reward points if useful
Thanks & Regards
Kiran -
How to Find out the all tables in module wise ?
Dear Friends,
1.How to Find out the all tables in module wise ?
what are the total number of table in SAP ?
2. how to find out all existing functions in SAP ?
Thanks and regards,
Subasha Chandra Sahoo.Hi,
You will get the module wise list from:
http://www.sourceveda.com/SAPReference.htm
http://www.sourceveda.com/
Regards... -
H how to find if the psa table is included in psa deletion process chain
Hi all
can anybody tell me .
I have a psa table it's technical name is /BIC/B*
h how to find the above psa table is included in psa deletion process chain or not
please help meHi
Ope the PC in RSPC1 -->go to planning view of process chain
in left side you will find different types of process types.
under other BW process types folder -->you will find process type "Delete request from PSA"
drag this into your process chain planning view and customize it based on your requirements.
check the below article for step by step procedure
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/a02ba9e7-bb6f-2c10-09b4-e86b9fcbad41?QuickLink=index&overridelayout=true
Regards,
Venkatesh -
How to find the columns and tables used in a stored procedure?
Hi,
Can someone suggest how to find the columns and tables used within a stored procedure?
Thanks
VBKFor example:
SQL> create or replace
2 procedure p1
3 is
4 cnt number;
5 begin
6 select count(*) into cnt from emp;
7 end;
8 /
Procedure created.
SQL> select referenced_owner,
2 referenced_name
3 from dba_dependencies
4 where owner = 'SCOTT'
5 and name = 'P1'
6 and referenced_type = 'TABLE'
7 /
REFERENCED_OWNER REFERENCED_NAME
SCOTT EMP
SQL> SY. -
How to find the count of tables going for fts(full table scan in oracle 10g
HI
how to find the count of tables going for fts(full table scan) in oracle 10g
regardsHi,
Why do you want to 'find' those tables?
Do you want to 'avoid FTS' on those tables?
You provide little information here. (Perhaps you just migrated from 9i and having problems with certain queries now?)
FTS is sometimes the fastest way to retrieve data, and sometimes an index scan is.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:9422487749968
There's no 'FTS view' available, if you want to know what happens on your DB you need, like Anand already said, to trace sessions that 'worry you'. -
Hi,
How to find the average of table row values it should display in next row in libwindow/CVI
Please let me know the solution.There isn't a built-in function to perform calculations on thable cells. What you can do is to retrieve thable cells values and calculate the average by yourself.
To retrieve a bunch of cells in a single instruction you can use GetTableCellRangeVals: prerequisite for this function to work correctly is that cells are all included in a Rect structure (shortly, a rectangle) and are all of the same data type. See the help for the function for some explanations and the link to an example of its usage. In Cell range parameter you can pass VAL_TABLE_ROW_RANGE (row) macro to retrieve an entire row. See here for details.
Once you have retrieved cell values in an array, you can pass it to Mean function to calculate the average.
Proud to use LW/CVI from 3.1 on.
My contributions to the Developer Zone Community
If I have helped you, why not giving me a kudos? -
How to check relation between two tables in same database
How to check relation between two tables in same database using Oracle SQL developer. Version 2.1.1.64
Hi,
Try this,
SELECT cons.owner AS child_owner, cons.table_name AS child_table,
cons.constraint_name constaint_name,
cons.constraint_type constraint_type, col.owner parent_owner,
col.table_name parent_table, col.column_name column_name
FROM dba_cons_columns col, dba_constraints cons
WHERE cons.r_owner = col.owner
AND cons.r_constraint_name = col.constraint_name
AND col.owner = 'MY_USER'
ORDER BY child_table;Thanks,
Shankar -
How to find last accessed/updated tables and the query text?
I am using :
Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
With the Partitioning option
JServer Release 8.1.7.4.0 - Production
How to find last accessed/updated tables and the query text?
Regards
LEE1212Check DBA_TBALES view there you find one date column that indicate last update
One option is as follows:
(1) Turn the auditing on: AUDIT_TRAIL = true in init.ora
(2) Restart the instance if its running.
(3) Audit the table:
AUDIT INSERT,SELECT,DELETE,UPDATE on TableName
by ACCESS WHENEVER SUCCESSFUL
(4) Get the desired information using :
SELECT OBJ_NAME,ACTION_NAME ,to_char(timestamp,'dd/mm/yyyy , HH:MM:SS')
from sys.dba_audit_object.
Cheer,
Virag Sharma
http://virag.sharma.googlepages.com/
http://viragsharma.blogspot.com/
Message was edited by:
virag_sh -
How to insert data into two tables linke with foreign key..
I have two tables
1)EMP(emp_ID,username,emp_type_code)
emp_ID is primary key, emp_type_code is a foreign key references emptype table.
2)emptype(emp_type_code,emp_type_descripton)
emp_type_code is primary key
Could anyone help me ..how to insert data into EMP table. How to insert data into two tables linke with foreign key..CREATE TABLE "CATDB"."DWDIMUSER"
"USER_ID" NUMBER(10,0) NOT NULL ENABLE,
"SPECIALTY_ID" NUMBER(10,0),
"FULLNAME" VARCHAR2(20 BYTE),
"FNAME" VARCHAR2(20 BYTE),
"LNAME" VARCHAR2(20 BYTE),
"USER_SUBTYPE" VARCHAR2(20 BYTE),
CONSTRAINT "DIMUSER_PK" PRIMARY KEY ("USER_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CATDB" ENABLE,
CONSTRAINT "DIMUSER_DIMSPECIALTY_FK" FOREIGN KEY ("SPECIALTY_ID") REFERENCES "CATDB"."DWDIMSPECIALTY" ("SPECIALTY_ID") DISABLE
CREATE TABLE "CATDB"."DIMSPECIALTY"
"SPECIALTY_ID" NUMBER(10,0) NOT NULL ENABLE,
"SPECIALTY_NAME" VARCHAR2(100 BYTE),
CONSTRAINT "DIMSPECIALTY_PK" PRIMARY KEY ("SPECIALTY_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CATDB" ENABLE
INSERT INTO DIMUSER (FullName, FNAME, LNAME, USER_TYPE, USER_SUBTYPE)
SELECT DISTINCT
Engineer AS FullName,
regexp_substr(Engineer , '[^,| ]+', 1, 1) as FName,
regexp_substr(Engineer , '[^,| ]+', 1, 2) as LName ,
'Engineer'
FROM EMPLOYEELOOKUP;
INSERT INTO DIMSPECIALTY (SPECIALTY_NAME)
SELECT DISTINCT SPECIALITY
FROM EMPLOYEELOOKUP;
COMMIT;
CREATE TABLE employeelookup ...IS A TABLE THAT HAS ALL THE DATA NEDED TO BE FILLED IN BOTHE TABLES...
CREATE TABLE "CATDB"."EMPLOYEELOOKUP"
"EMPLOYEELOOKUP_ID" NUMBER(10,0) NOT NULL ENABLE,
"ENGINEER" VARCHAR2(25 BYTE),
"SPECIALTY" VARCHAR2(20 BYTE),
CONSTRAINT "DIMSPECIALTY_PK" PRIMARY KEY ("EMPLOYEELOOKUP_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "CATDB" ENABLE
DATA IN EMPLOYEELOOKUP
Engineer, Specialty,
John, Dow, Electronis,
Dow, Jons, Technician
Stan Smithers Sales
Mark, Richards Marketing
Jenny, Lane Marketing
John, Lee Sales
I NEED TO LOAD THE FOREIGN KEY IN DIMUSER FROM THE DIMSPECIALTY TABLE?
BY USING THE LOOKUP TABLE TO MARCH THE NAMES UNDER THE Engineer COLUMN, SPECIALTY COLUMNE DISTICTIVLY BY JOINING THE DIMSPECILTY TO RISTIVE THE PRIMARY KEY AND FILL IT IN THE DIMUSER TABLE AS A FOREIGNE KEY.
Maybe you are looking for
-
How come the form in the JSP page does not work if user hits "ENTER"
When I use the following code for loginpage.jsp If the user hits ENTER on the keyboard it just displays the same form with nothing in the username box. It works if the user clicks the button. <HTML> <BODY> <% String submit = request.getParameter("sub
-
Leopard OS 10.5 Finder no longer shows other computers on my network
When I first got this MacBook Air every time I opened Finder, there was a section called SHARED and a list of other networks and computers on my wireless networks at home and work. I could easily access shared docs on my co-workers' and husband's PCs
-
.HTML and .DWT files in folders have 0kb size
Hey guys, I was just setting up a new client on the system and I have an issue with uploading HTML and DWT files through Dreamweaver. The files end up on the server but each is a 0kb file. Here are my steps: In Partner Portal I created a new site I
-
Checksum Error after downloading
Hi, I purchased as a bundle both Adobe Elements 10 and Photoshop Elements together at the half price special. Adobe Elements 10 installed just fine with serial number provided for me...however Photoshop Elements downloads and upon completion a pop-up
-
Download is stuck! Frustrating!!!!
Just bought a subscription for photoshop and I have been trying to download Creative CLoud Desktop for three days. The download starts and then sits there and doesn't finish downloading. I am losing days on my monthly subscription. Should not be this