Compare two table columns
hi all,
i am using db10g.
my task is to compare two table's data for example
table1 is having
col1 col2 col3
a b c
e f g
table2 is
col4 col5 col6
a e c
e f g
so i have to compare col1 first record and col4 first record.
in otherwords first table first column first item with second table first table first value and so on.
for ex: a= a
b= e
c= c etc
how can i acheive this?
both are database tables.
i cannot use minus function because data types are different for the corresponding column
how can i compare it?
Thanks..
Edited by: user13329002 on Jan 13, 2011 1:09 AM
Hi,
Try this
select
case
when a.col1 = b.col1 then 'EQUAL' else 'NOT EQUAL' end column1,
case
when a.col2 = b.col2 then 'EQUAL' else 'NOT EQUAL' end column2,
case
when a.col3 = b.col3 then 'EQUAL' else 'NOT EQUAL' end column3
from (select col1,col2,col3,rownum rn from table1) a,
(select col1,col2,col3,rownum rn from table2) b where a.rn = b.rn Try to see how you order the rows of the two tables.
cheers
VT
Similar Messages
-
hi,
Does anyone have or can show me the principle of a such kind of function ?see
http://geekswithblogs.net/leonardm/archive/2010/01/14/table-schema-comparison-in-sql-server.aspx
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
Compare two tables and log the difference
Hi,
I want to compare two tables and log the difference in new table with the fields as (old value,new value, column name). The column name should be the changes value columnCan you show an example?
SELECT Foo.*, Bar.*
FROM Foo
FULL OUTER JOIN
Bar
ON Foo.c1 = Bar.c1
AND Foo.c2 = Bar.c2
AND Foo.cn = Bar.cn
WHERE Foo.key IS NULL
OR Bar.key IS NULL;
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Comparing Two tables with 300k records and update one table
Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
Column B has same data in both the tables.
I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
I m trying to do using PLSQL Tables.
Any suggestion?
Thanks.Hi,
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved, so that the people who want to help you can re-create the problem and test their ideas.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
If you're asking about a DML statement, such as UPDATE, the CREATE TABLE and INSERT statements should re-create the tables as they are before the DML, and the results will be the contents of the changed table(s) when everything is finished.
Always say which version of Oracle you're using (for example, 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002
ef2019c7-080c-4475-9cf4-2cf1b1057a41 wrote:
Could you let me know how to compare two tables having 300k records and update one table.below is the scenario.
Table Tabl_1 has columns A,B and Tabl_2 has columns B,new_column.
Column B has same data in both the tables.
I need to update Tabl_2 in new_column with Tabl_1 A column data by comparing B column in both tables.
I m trying to do using PLSQL Tables.
Any suggestion?
Thanks.
Why are you trying to use PL/SQL tables? If tabl_1 and tabl_2 are regular database tables, it will be much simpler and faster just to use them.
Depending on your requirements, you can do an UPDATE or MERGE, either in SQL or in PL/SQL. -
BUG? BRIDGE statement to compare two tables
Hello,
I tried to compare two tables in different dbs and remembered a post about Cross Connectin Queries
http://barrymcgillin.blogspot.com/2010/11/cross-connection-queries.html
BRIDGE temparb AS "EB05 01"
(SELECT * FROM arb)
(SELECT * FROM temparb
MINUS
SELECT * FROM arb
UNION ALL
(SELECT * FROM arb
MINUS
SELECT * FROM temparb
);I would expect to get the differences between the table arb in my current schema and the table arb alias temparb in the other db. Yet it seems, that in this case only the table in my current schema is read.
I materialize the BRIDGE table with
BRIDGE temparb AS "EB05 01"
(SELECT * FROM arb)and select the value I know to be only in the remote schema
SELECT id FROM temparb WHERE id = 2562;
SELECT id FROM arb WHERE id = 2562;Both times I get no result. I even tried to use aliases on the table, same result. Tested in 3.0 and 3.1 EA3.
Has anyone tried this before?
Regards
MarcusHi Marcus,
Have you tried it without the double quotes around the connection name?
I have a connection called
system_local
which I run the following command in
drop table testbridge_remote;
drop table testdbrige;
create table testbridge(col1 int);
insert into testbridge values (1);
insert into testbridge values (2);
insert into testbridge values (3);
commit;
I have a connection called
Connection Name With Space
which I run the following in
drop table testdbrige;
create table testbridge(col1 int);
insert into testbridge values (4);
insert into testbridge values (5);
insert into testbridge values (3);
commit;
Then in the system_local connection/worksheet I can run the following statement
BRIDGE testbridge_remote as Connection Name With Space(select * from testbridge)
+(SELECT * FROM testbridge_remote+
MINUS
SELECT * FROM testbridge
+)+
UNION ALL
+(SELECT * FROM testbridge+
MINUS
SELECT * FROM testbridge_remote
+);+
It works ok.
Returning rows in the remote table not in the local table , and rows in the local table not in the remote table .
Regards,
Dermot
SQL Developer Team. -
how to compare two tables between two systems ?
hi sravan, u can go to t/c OY19 to compare the two tables.
Choose Manual selection
Enter the name of the table that you want to compare
Choose Execute
in the next screen you have to give the description and RFC connection name which will make the link between two clients.
Regards...
Arun.
Reward points if useful. -
HI,
How to compare two tables in ODI.I want to extract UNMATCHED data in the two tables without using procedures,views.Hi,
these tables has the exactly same ddl or the comparacion will be based on PK (or UK)? -
How to compare two tables in SSIS?
I am creating an SSIS package that will compare two tables and then insert data in another table.
Which tool shall I use for that? I tried to use "Conditional Split" but it looks like it only takes one table as input and not two.
These are my tables:
TABLE1
ID
Status
TABLE2
ID
Status
TABLE3
ID
STatus
I want to compare STATUS field in both tables. If Status in TABLE1 is "Pending" and in TABLE2 is "Open" then insert this record in TABLE3.You can use MERGE command , Sorry cannot test it right now.
MERGE INTO tbl3
USING (SELECT tbl2.ID, tbl2.status,tbl1.status as st1 FROM tbl1 JOIN tbl2 ON tbl1.ID=tbl2.ID) AS t ON T.ID=tbl3.ID
AND t.status='OPEN' AND t.st1='pending'
WHEN NOT MATCHED
INSERT (status) VALUES (t.status)
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Hi
Using below sql get the columns discrepncy .I want to display the discprency of columns from two tables in form of flat file using pl/sql.
select col1,col2 from A
minus
select * from B -- Rows in A that are not in B
union all
select col1,col2 from B
minus
select col1,col2 from A ----- rows in B that are not in A
Thanksin advance
MRHi
Wrote the cursor for this ,I am not getting expected o/p .Please any help
DECLARE
CURSOR C1 IS SELECT ACCOUNT_CUST_CD , account_contact_person_cd
FROM account where ACCOUNT_CUST_CD='1-411FQ'
AND account_contact_person_cd IS NOT NULL ORDER BY account_contact_person_cd ;
CURSOR C2 IS SELECT ACCOUNT_CUST_CD , account_contact_person_cd FROM bill_bkp
where ACCOUNT_CUST_CD='1-411FQ'AND ACCOUNT_STATUS_DESC='Blacklisted Customer'ORDER BY account_contact_person_cd ;
BEGIN
FOR REC1 IN C1
LOOP
FOR REC2 IN C2
LOOP
IF REC1.account_contact_person_cd <> REC2.account_contact_person_cd or
REC1.ACCOUNT_CUST_CD<>REC2.ACCOUNT_CUST_CD
THEN
DBMS_OUTPUT.PUT_LINE(REC1.account_contact_person_cd||REC2.account_contact_person_cd||REC1.ACCOUNT_CUST_CD||REC2.ACCOUNT_CUST_CD);
END IF;
END LOOP;
END LOOP;
END;
Thanks and Regards,
MR -
Compare two tables having different number of rows based on 2 columns
Hi,
I am having two tables table a having field1, field2 and table b having fields field1 and field2.
I want those records from table a in which field1 of table a is not matching to field1 of table b and field2 of table b not matching to field2 of table b, but i also want the bifurcation of records as whether field 1 is not matching, field 2 is not matching or both fields 1 & 2 are not matching.
e.g.
table a table b
field1 field2 field1 field2
1 6 12 5
2 5 1 9
13 51 13 51
45 31 99 121
33 45
In this case my output should be
table a
field1 field2 Mtchng_Field
1 6 Field 2 not mtchng
2 5 Field 1 not mtchng
45 31 Feild1 and Field2 both not matching
How would i get my result in the required format.sql>select * from t1;
N1 N2
1 6
2 5
13 51
45 31
33 45
sql>select * from t2;
N1 N2
12 5
1 9
13 51
99 121
sql>
select n1,n2,decode(nvl(p1,0)+nvl(p2,0),0,'No match',1,'F2 not match',2,'F1 not match','Match') status
from(
select n1,n2,(select 1
from t2 where n1=t1.n1) p1,(select 2
from t2 where n2=t1.n2) p2
from t1);
N1 N2 STATUS
1 6 F2 not match
2 5 F1 not match
13 51 Match
45 31 No match
33 45 No match
Message was edited by:
jeneesh
Message was edited by:
jeneesh
Some problems... -
Is there an easy way to compare two null columns?
I need to compare a significant number of columns between records in two tables for a data conversion. I really need a comparison that will return true if: 1) both columns are null; or 2) both columns are not null and equal. I want it to return false if: 3) one column is null and the other is not; or 4) both columns are not null and are not equal. I am trying to find records which are not exact matches.
I found documentation at oracle-base.com about the SYS_OP_MAP_NONNULL function that would do what I want, but I don't want to use this since it's an undocumented feature and my code will be in production for a period of time.
I would rather not have to use a construct like this for each and every column I'm comparing:
a.col is null
and b.col is null
or (
a.col = b.col
)Also, I know about the NVL function, but I'm comparing columns which are entered by users, and I'm not comfortable substituting any values for null because those values might actually exist in the data.Performance wasn't the issue but they will be about the same anyway. The issue was avoiding the messy syntax needed to do the job.
Which of these looks easier to you if you had to understand and maintain the code or add new columns
>
I would rather not have to use a construct like this for each and every column I'm comparing:
a.col is null
and b.col is null
or (
a.col = b.col
>
or
1 = DECODE (a.col, b.col, 1)So if, like the OP, you had to 'compare a significant number of columns' which syntax would you use? -
Stored Procedure to compare two tables data
Hello,
I want to have a Stored Procedure which will compare data (only a single column) of two tables and delete the row which is not present in the second table.
For eg.,
I have got 2 tables called Table1 and Table2, which have the same column called ID(PK).
The data in Table1 is 1,2,3 and data in Table2 is 1,3. When the stored procedure runs, it has to compare the ID column in Table1 to ID column in Table2 and since '2' is not there in Table2, I want to delete this row from Table1.
Thanks in advance.user11281601 wrote:
Hello,
I want to have a Stored Procedure which will compare data (only a single column) of two tables and delete the row which is not present in the second table.
For eg.,
I have got 2 tables called Table1 and Table2, which have the same column called ID(PK).
The data in Table1 is 1,2,3 and data in Table2 is 1,3. When the stored procedure runs, it has to compare the ID column in Table1 to ID column in Table2 and since '2' is not there in Table2, I want to delete this row from Table1.
Thanks in advance.
delete from table1 where id_column in
(select id_column from table1
minus
select id_column from table2); -
Comparing two tables and picking the records
I have two tables A and B. I have to compare a column ID in both table A and table B A.ID=B.ID And based on the above comparison if the match is found I need to pick the records from table A and if no match then need to pick the records from table B My problem is if I join then I will have table A and table B as one record and will be difficult to separate out. Please let me know how this can be acheived. Thnx.
I have two tables A and B. I have to compare a column ID in both table A and table B A.ID=B.ID And based on the above comparison if the match is found I need to pick the records from table A and if no match then need to pick the records from table B My problem is if I join then I will have table A and table B as one record and will be difficult to separate out. Please let me know how this can be acheived. Thnx.
-
How to compare two tables data...need sql report or utility to find differe
Hi,
We have a requirement where we are asked to find data differences between two tables and one of the tables reside on remote database. The database version is same ( 10g ) and datatypes for the tables are similar.
The client is looking for a sql report or kind of utility to display the data differences for each column ( if possible count differences ) with some meaningful error messages.
Could anyone let me know the best possible way of doing it..?
Thanks
HitarthHi,
I found something for tables comparison but getting one error...can you check this please and let me know what is wrong
Here is the function:
CREATE OR REPLACE FUNCTION compare_query_results (
p_query1 IN VARCHAR2
, p_query2 IN VARCHAR2
, p_raise_error_if_not_equal IN BOOLEAN DEFAULT FALSE
, p_raise_error_if_no_rows IN BOOLEAN DEFAULT FALSE
RETURN NUMBER
IS
-- Constants
c_query_results_equal CONSTANT PLS_INTEGER := 0;
c_query_results_not_equal CONSTANT PLS_INTEGER := 1;
oracr CONSTANT VARCHAR2 (1) := CHR (10);
-- Variable Declaration
v_sql_stmt VARCHAR2 (32767);
v_record_count PLS_INTEGER;
v_return_code PLS_INTEGER;
v_record DUAL.dummy%TYPE;
v_result_set_has_rows BOOLEAN;
-- Ref Cursors
v_cursor sys_refcursor;
-- Custom Defined-Exceptions
result_sets_do_not_match EXCEPTION;
query_returns_no_rows EXCEPTION;
BEGIN
-- Get the count of differing records between p_query1 and p_query2
dbms_output.put_line('Start-1');
v_sql_stmt :=
' (SELECT /*+ materialize */'
|| SUBSTR (p_query1, INSTR (UPPER (p_query1)
, 'SELECT'
, 1
, 1
) + 6)
|| ')
, (SELECT /*+ materialize */'
|| SUBSTR (p_query2, INSTR (UPPER (p_query2)
, 'SELECT'
, 1
, 1
) + 6)
|| ')
SELECT ''X''
FROM (
(SELECT * FROM test1 MINUS SELECT * FROM test2)
UNION ALL
(SELECT * FROM test2 MINUS SELECT * FROM test1)
dbms_output.put_line('Start-2');
OPEN v_cursor
FOR v_sql_stmt;
dbms_output.put_line('Start-3');
FETCH v_cursor
INTO v_record;
dbms_output.put_line('Start-4');
v_result_set_has_rows := v_cursor%FOUND;
dbms_output.put_line('Start-5');
CLOSE v_cursor;
dbms_output.put_line('Start-6');
-- If there are rows - the result sets do NOT match...
IF v_result_set_has_rows
THEN
v_return_code := c_query_results_not_equal;
IF p_raise_error_if_not_equal
THEN
RAISE result_sets_do_not_match;
END IF;
-- If there are no rows - the result sets do match...
ELSIF NOT v_result_set_has_rows
THEN
IF p_raise_error_if_no_rows
THEN
-- Check to make sure that the queries contain rows if desired...
v_sql_stmt := 'SELECT ''X''
FROM (' || oracr || p_query1 || oracr || ')';
OPEN v_cursor
FOR v_sql_stmt;
FETCH v_cursor
INTO v_record;
IF v_cursor%NOTFOUND
THEN
CLOSE v_cursor;
RAISE query_returns_no_rows;
END IF;
CLOSE v_cursor;
END IF;
v_return_code := c_query_results_equal;
END IF;
RETURN v_return_code;
EXCEPTION
WHEN result_sets_do_not_match
THEN
raise_application_error (-20101, 'The Queries'' result sets do NOT match. Error returned
as requested.');
WHEN query_returns_no_rows
THEN
raise_application_error (-20102, 'The Queries'' result sets match, however they contain no
rows. Error returned as requested.');
WHEN OTHERS
THEN
-- Raise the error
raise_application_error (-20103
, 'There is a syntax or semantical error in one or both queries
preventing comparison.'
|| oracr
|| 'Error Stack :'
|| oracr
|| DBMS_UTILITY.format_error_stack ()
|| oracr
|| 'Error_Backtrace:'
|| oracr
|| DBMS_UTILITY.format_error_backtrace ());
END compare_query_results;
I have created two tables ( test1 and test2 ) with few columns and with the same datatypes and executed the above function...I am getting error as folliowing:
DECLARE
ERROR at line 1:
ORA-20103: There is a syntax or semantical error in one or both queries
preventing comparison.
Error Stack :
ORA-00900: invalid SQL statement
Error_Backtrace:
ORA-06512: at "ORAOWNER.COMPARE_QUERY_RESULTS", line 53
ORA-06512: at "ORAOWNER.COMPARE_QUERY_RESULTS", line 121
ORA-06512: at line 12
Could someone please help me fixing this error..It would be really appreciated
Thanks
Hitarth -
Hello!
I have one table in ERP6.0 system, let's say T082G that is different in DEV and in PRD system after the Upgrade. The difference is the one table has more activated radio buttons than the other one.
How can I compare the two tables and fix the problem?
Which tcode can be applied here?
Thank you very much!
regards
ThomHi,
Compare a contents of a table between clients.
and also, The contents of a table between two systems can be checked through the transaction OY19
Regards
Maybe you are looking for
-
How to get wage type for every time record
Hi Pros, I am using DS 0CA_TS_IS_1, it includes report time type (0REPTT), but not have wage type. in CATSDB, I fied fields for attendance/absence type and wage type. but not every time record has wage type. can you please tell me how to get wa
-
Sales analysis report - in query form?
Good afternoon. Looking for a bit if help/advice. I'm not a complete novice to SAP but what I need is confusing me slightly. I understand the sales analysis report uses many different table as part of its calculations. A company I've recently began w
-
Insert a link on xml report. The link should open up a document(PDFS).
Is it possible to insert a link in the report? The value would come from a sql query. The link when clicked on would open up the pdf or doc. Is this done thru a href or by inserting a dynamic hyper link. The release Release 5.6.2 documentation refere
-
Lightroom "folders" section, eveything disappeared, how do I get the files back?
Technically I believe the files are still within lightroom, and still on my hard drive. Under Catalog "All Photographs" it has all the ones I've ever imported to Lightroom. I was doing something and right clicked under the folders tab, thinking I was
-
Background music (Mobile)
hello, I would like to play a background music for any application that does not interrupt the exchange of views, and that does not happen again if I go back to the view where I didplay. I inserted this code in the mxml file: <s:TabbedViewNavigatorAp