Compare data between two tables
Hey Experts
I am having two tables both are having same structure.
Both r having 210 columns
I want to compare data between these two tables.
I used follwoing query
select * from t1
MINUS
select * from t2but even if thr is diff in 1columns .. i need to search 210 columns which is the exact columns and data
how can i find the exact columns out of these 210 columns ?
SShubhangi wrote:
but even if thr is diff in 1columns .. i need to search 210 columns which is the exact columns and data
how can i find the exact columns out of these 210 columns ?This is not a trivial problem to solve. Consider a much simpler data set. In table 1 (four columns) we have
1 A B C
2 D E F
3 A B Dand in In table 2 (four columns) we have
2 X Y Z
3 A B ENow
select * from table1 minus select * from table2gives
1 A B C
2 D E F
3 A B Dwhereas
select * from table2 minus select * from table1gives
2 X Y Z
3 A B ENow clearly the first row in Table1 (ID=1) doesn't match any row in Table2 but it's only two columns out from the last row in Table2 (ID=3). However, the row in Table1 with ID=3 only doesn't match on one column.
So, how do you represent the output?
Clearly what you want can only be achieved if there are some columns which ought to be the same in both tables i.e. key columns. In which case you can use a full outer join link teh two tables, and case statements to display only the values which don't match:
select t1.id as t1_id
, t2.id as t2_id
, case when t1.col1 != t2.col1 then r1.col1 end as t1_col1
, case when t1.col1 != t2.col1 then r1.col1 end as t2_col1
, case when t1.col2 != t2.col2 then r1.col2 end as t1_col2
, case when t1.col2 != t2.col2 then r1.col2 end as t2_col2
, case when t1.col3 != t2.col3 then r1.col3 end as t1_col2
, case when t1.col3 != t2.col4 then r1.col4 end as t2_col2
from table1 t1
full outer join table2 t2
on t1.id = t2.id;Handling nulls is left as an exercise for the reader :)
I agree that typing all this would be extremely tedious for a table with 210 columns, but that's why Nature gave us the ability to generate SQL statements from teh data dictionary.
Cheers, APC
Similar Messages
-
How to compare data between two tables?
Hi,
My team is trying to develop a SAP data migration tool (DMT) using ABAP.
One of the functionalities in the DMT is to validate the data in the staging area against the loaded SAP data.
The tables in the stagin area are customer tables (i.e. user-defined tables starting with Y, Z).
How do I compare the data in the staging area against data that are loaded into SAP tables? Are there some built-in SAP functions to do this? Or, are there some better ways of doing this (e.g. instead of comparing against data in the SAP tables, we compare with some INTERNAL tables)?
Any help would be greatly appreciated, thanks!Hi Kian,
Use <b>SCMP</b> transaction to compare data between two tables and you can not use this for comparing internal tables.
Thanks,
Vinay -
Compare data between two tables of same schema
Folks,
I have one very intresting query which i would like to share with you all and looking forward for the solution asap.
Scenario
I have two table say TableA and TableB, both having same structre say as below
TableA
Col1 Var(10)
Col2 INT
TableB
Col1 Var(10)
Col2 INT
I want to compare data between these two tables and store compared data into third table, let me expalin the whole scenario.
TableA
ColA ColB
INDIA 1
PAKistan 2
TableB
ColA ColB
INDIA 1
PAK 3
I want result like
Difference
ColA ColB
True 0
False -1
I want to store this difference in thrid table.
i.e. when comparing text, i need TRUE when compare 100% else False, Caption is not considered.
When comparing numeric value, simple sub is requried , TableA-TableB
Note - I dont want to use any external tool to compare the table data, i required sql query to do the same.
Thanks
Amit Srivastava
Amit
Please mark as answer if helpful
http://fascinatingsql.wordpress.com/Whereas the abbreviation of countries that exist in Table2 table are the first three letters of the name of the country*, here's a suggestion:
-- code #1 v2
INSERT into [Difference] (Col1, Col2, ACol1, BCol1)
SELECT case when A.Col1 = B.Col1 then 'true' else 'false' end,
(IsNull(A.Col2, 0) - IsNull(B.Col2, 0)), A.Col1, B.Col1
from TableA as A full outer join
TableB as B on (A.Col1 = B.Col1
or Left(A.Col1, 3) = B.Col1);
Is the COLLATE database case insensitive? If not, the code #1 above will have to be modified, using the upper () function or using COLLATE case insensitive in A.Col1 and B.Col1 columns.
But if the abbreviation of the country follow the
ISO 3166-1 alpha-3 standard, will require a fourth table containing the symbol and name of countries.
-- code #2 v2
;with
TableB_2 as (
SELECT case when Len(Col1) = 3
then (SELECT Country_name from [ISO 3166-1 a3] where Cod = Col1)
else Col1 end as Col1, Col2
from TableB
INSERT into [Difference] (Col1, Col2, ACol1, BCol1)
SELECT case when A.Col1 = B.Col1 then 'true' else 'false' end,
(IsNull(A.Col2, 0) - IsNull(B.Col2, 0)), A.Col1, B.Col1
from TableA as A full outer join
TableB_2 as B on A.Col1 = B.Col1;
Structure and data to test:
use tempdb;
CREATE TABLE TableA (Col1 varchar(10), Col2 int);
CREATE TABLE TableB (Col1 varchar(10), Col2 int);
CREATE TABLE [Difference] (Col1 varchar(10), Col2 int, ACol1 varchar(10), BCol1 varchar(10));
INSERT into TableA values ('INDIA', 1), ('PAKistan', 2), ('China', 12);
INSERT into TableB values ('INDIA', 1), ('PAK', 3), ('Bhutan', 3);
go
CREATE TABLE [ISO 3166-1 a3] (Cod char(3) primary key, [Country_name] varchar(30));
INSERT into [ISO 3166-1 a3] values
('IND', 'India'), ('PAK', 'Pakistan'), ('CHN', 'China'), ('BGD', 'Bangladesh'),
('BTN', 'Bhutan'), ('MMR', 'Myanmar'), ('NPL', 'Nepal');
go
(*) If the short form of the country name using the first three letters of the country name,
false positives can occur. For example,
Mali and Malta or
Angola and Anguilla.
José Diz Belo Horizonte, MG - Brasil -
Compare data between two tables all coullms
Hi All,
I have a task to compare data for the dev and prod data for the same table ,can some one please help me with the script for this ...
Thanks in Advance..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/
Blog : MS SQL Development and Optimization
Blog : Large
scale of database and cleansing -
Comparing Data between two tables with different structure
Hi,
I have 2 tables T1 and T2. Both tables have different structure. I have to check for rows missing in T1 which exist in T2 and vice-versa. I can do that using MINUS operator. But the part where the data exists in both T1 and T2, I have to compare some columns (9 columns the names of this column coming from a meta-data table) if they have same values. If not an exception has to be generated.
Any help is appreciated.
ThanksHi,
Whenever you need help on this forum, post:
(1) The version of Oracle (and any other relevant software) you're using
(2) A little sample data (just enough to show what the problem is) from all the relevant tables
(3) The results you want from that data (4) Your best attempt so far
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
Formatted tabular output is okay for (3). Type before and after the tabular text, to preserve spacing.
What do you mean by "an exception has to be generated"?
Do you want to raise an error?
Do you want something to appear in the result set?
MINUS finds rows that are in one result set, but not in another.
INTERSECT finds rows that are in both result sets.
Column names have to be hard-coded into the SQL statement. If you want to write something now that will get column names from a metadata table sometime in the future, then you have to use dynamic SQL. SQL*Plus sometimes has quick and dirty ways of doing dynamic SQL, so say whether you're using SQL*Plus or not. -
Query the data between two tables
Need help for query the data between two tables
Table 1: Time sheet
P.ID P.Name EmpID HoursSpend DateTime
c12234 Test 25 4 06/12/2013
c12234 Test 25 7 06/13/2013
c12234 Test 25 8 06/15/2013
c12234 Test 5 3 06/21/2013
c12234 Test 2 5 07/15/2013
c12234 Test 25 4 07/21/2013
Table 2: cost table
EmpID FromDate ToDate Rate
25 05/01/2013 06/30/2013 250
2 04/01/2013 05/31/2013 150
25 07/01/2013 09/30/2013 300
Output
P.ID P.Name EmpID HoursSpend DateTime Rate Total (HoursSond x Rate)
c12234 Test 25 4 06/12/2013 250 1000 (4*250)
c12234 Test 25 7 06/13/2013 250 1750
c12234 Test 25 8 06/15/2013 250
2000
c12234 Test 25 4 07/21/2013 300
1200
c12234 Test 2 5 07/15/2013 150
750
===========================================
Total 28
6700
============================================
Here EmpID =2 don't have rate in the cost table on july month should be pick from last entry from cost table.Hi Gopal,
According to your description, it seems that the output needn’t include the row when EmpID=2. Because the DateTime for it in Table1 doesn’t included between FromDate column and ToDate column. After testing the issue in my environment, we can refer to the
query like below to achieve your requirement:
SELECT time.*,cost.EmpID,cost.Rate,(time.HoursSpend * cost.Rate)as [Total (HoursSond x Rate)]
FROM [Time sheet] as time
INNER JOIN
[cost table]as cost
ON time.EmpID = cost.EmpID
AND time.DateTime BETWEEN cost.FromDate AND cost.ToDate
If there are any other questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
Transaction to see common data between two tables
Is there any transaction to see common data between two tables with out creating views.
what is the transaction to see the link between two tables
regards
pavanHi,
Go to a transparent table for which you want to know the common fields, from there click the button GRAPHICS (shortcut Ctrl + Shift + F11) in the application tool bar. Then you are redirected to a list containing the tables belonging to the same group. There select whatever tables you would like to see and click COPY. A window will pop-up and will show the relationship between the fields of the tables in a flow chart format.
see to this link also.
Common fields b/w tables
Regards,
Revathi Bhoopal. -
How to compare dates between two items
Hello all,
I am trying to compare dates in two items.
The first item is a computation P45_PAGE_LOADED_TIME
select sysdate from dual;
This essentially keeps track of when the user opened the form. They are accessing data that may become obsolete while they are in the form.
So we have a dynamic action that finds the last_update date from a table. This field is of type date. It is placed in a text field called P45_STATUS.
We then have a notification which we want to fire when P45_PAGE_LOADED_TIME is before P45_STATUS. We tried the following condition
declare
least_date date;
begin
SELECT LEAST(TO_DATE(:P45_STATUS),TO_DATE(:P45_PAGE_LOADED_TIME)) into least_date
from dual;
if least_date = to_date(:P45_STATUS) then
return false;
else
return true;
end if;
end;
Any guidance would be most appreciated.
ThanksMaybe your better off putting your anonymous block into a plsql function.
A function that you can test. If it works in plsql then you must call it in APEX.
I kinda get a headache when I see that least, to_date, to_date query (although I've seen worse :p)
Make it like:
create or replace function fun_least_date(in_status in varchar2,
in_page_loaded_time varchar2) return boolean
is
l_status date;
l_page_loaded_time date;
least_date date;
l_return boolean;
begin
l_status := to_date(in_status, 'FORMAT MASK!!!');
l_page_loaded_time := to_date(in_page_loaded_time, 'FORMAT MASK!!!!');
least_date := least(l_status, l_page_loaded_time);
if least_date = l_status then
l_return := false;
else
l_return := true;
end if;
return l_return
end fun_least_date;If your sure that your function is correct then use it as a condition for your dynamic action.
Regards
Nico
ps: I haven't tested that function. Be aware that you most correctly enter the format mask you use in APEX. Also before APEX may know about these variables you should set a dynamic action on your items to always submit their value to the server when they change!!! -
Comparing data from two tables
My PL/SQL is very limited, however, I need to write some code that compares data stored in two different Oracle tables and then writes the differences to a third table. Both tables have primary keys that I will use to ensure I am comparing like for like. These tables could be large.
Any help on the best way to do this, or any code examples would be very appreciated.One fairly efficient way of doing this is with set operators. The following code is untested - but looks right ;). It also assumes you want to know the provenance of the rows; if not, just zap the literal from each SELECT statement.
INSERT INTO diff_tab SELECT * FROM
( (SELECT 'tab1nottab2' AS provenance, t1.col1, t1.col2 FROM tab1 t1
MINUS
SELECT 'tab1nottab2', t2.col_a, t2.col_b FROM tab2 t2)
UNION ALL
(SELECT 'tab2nottab1', t2.col_a, t2.col_b FROM tab2 t2
MINUS
SELECT 'tab2nottab1', t1.col1, t1.col2 FROM tab1 t1)
/Cheers, APC -
Transferring data between two tables - Best practise advice
Hi!
I need advice on best practise since I am new to abap-thinking.
I have two tables. I am going to transfer data from table1 and update the corresponding master data table with the data in table1.
Which is the best way of doing this? The data amount that can be transferred is maximum 300 000 rows in table1.
I can only think in one, the simple, way which is to read all the rows in to an internal table and then do an update on all the rows in the master data table.
Is there a better way of doing this?
thanks in advance,
regards
BaranHi!
1. The update will be done a couple of times per week.
2. Yes, the fields are the same.
3. Both tables are SAP dictionary tables. One is a staging table and the other is master data table. Our problem is that we want a custom field to a standard master data table. We add an extra field to the staging table and the same to the corresponding master data table but the standard API is not supporting the transfer of data between custom fields so we are developing our own code to do this.
After some standard code has transferred the standard fields from staging tables to master data tables we are going to transfer our field by updating all the rows in the standard table
thanks
regards
Baran -
How to compare data between two worksheet in Excel for applescript
Hi All,
How to compare the data from two different worksheet in Excel and set the value into one worksheet according to the same name? Here is the example. Worksheet 1 & 2 current we have, the final worksheet is the result we want and the value can be input in worksheet 1. Much appreciate if you can help on it.
Worksheet 1:
Name Number
Leo 25
Jame 55
Leo 30
Jame 60
Tim 44
Tomas 77
Lyne 35
Tonny 66
Jame 22
Game 88
Worksheet 2:
Name Number 2
Leo 60
Jame 150
Tim 66
Tomas 88
Lyne 55
Tonny 99
Game 111
Rusult in Worksheet 1
Name Number Total Number per name in Worksheet 1 Number 2 in Worksheet 2
Leo 25 55 60
Jame 55 137 150
Leo 30 55 60
Jame 60 137 150
Tim 44 44 66
Tomas 77 77 88
Lyne 35 35 55
Tonny 66 66 99
Jame 22 137 150
Game 88 88 111I'd probably use a database for this, if there's any quantity of data involved here. Import from Excel into {SQLite, MySQL, PostgreSQL, FileMaker, maybe Core Data}, or pick your preferred key-value store, keep your data in the database, then export or (via ODBC/JDBC) then access live database data from within the spreadsheets.
Alternatively and if you're looking at small quantities of data (say, less than 10,000 entries, or less than a thousand depending on the language), then just use whatever passes for a key-value store in your preferred scripting language {Python, bash, Lua, or maybe php, AppleScript or Java} and use that. Export Excel to CSV {gag} or XML, then load that into Python and process as needed, then write out CSV {gag} or XML.
AppleScript is a scripting language for GUI applications, and also useful for processing events. If you're not doing that sort of stuff, then there can be other choices, and other choices can often have extensive frameworks and libraries for common tasks.
Sooner or later, most everybody runs into a wall when using a spreadsheet... Various folks have encountered those limits and have migrated from spreadsheets to FileMaker databases, and now use a database as the central store for their operations — and that's the other issue that can arise with spreadsheets... Where's the canonical data? -
Copying data between two tables with different structures
I have two tables. second table has three extra fields at the end. How can I copy data from first table to the other? I tried the following but it does not seem to work:
INSERT INTO second_table ((select * from first_table),'text','text',5)
Please help. ThanksINSERT INTO second_table SELECT col1, col2, col3, .., 'text', 'text', 5 FROM first_table;
Cheers!
r@m@ -
Exchange data between two tables
Hi
oracle 10.2
I have following case. One table which is reloaded on daily basis named DELTA. Second table DELTAH i want to use as the store of DELTA's previous loads.
Before every load of DELTA i want put data from it to DELTAH.
DELTA is partitioned by list by column f1 with three partitions. DELTAH is partitioned by list by job_id, where job_id is load identifier. DELTA and DELTAH have same columns.
I wanted to do something like exchange partition to fill DELTAH with DELTA data. But with the given structure i can't use exchange partition.
Now i can
1. Change partitioning of DELTAH to range-list, so it has range partitions by job_id and list subpartitions by f1 (like in DELTA). So i can exchange partitions of DELTA with subpartitions of DELTAH.
But i don't like this, because all these actions have to go in procedure and job_id can be arbitrary value. With arbitrary value i have either add or split partitions, whereas with list partitions i can only add them.
2. Do insert\select. But it is long time, i think.
Any other options?
May be i missed something, but i didn't get why Oracle has option to switch data between partition and table, but has no option to do it between table and table.Dear,
Hi
oracle 10.2
I have following case. One table which is reloaded on daily basis named DELTA. Second table DELTAH i want to use as the store of DELTA's previous loads.
Before every load of DELTA i want put data from it to DELTAH.
DELTA is partitioned by list by column f1 with three partitions. DELTAH is partitioned by list by job_id, where job_id is load identifier. DELTA and DELTAH have same columns.
I wanted to do something like exchange partition to fill DELTAH with DELTA data. But with the given structure i can't use exchange partition.
Now i can
1. Change partitioning of DELTAH to range-list, so it has range partitions by job_id and list subpartitions by f1 (like in DELTA). So i can exchange partitions of DELTA with subpartitions of DELTAH.
But i don't like this, because all these actions have to go in procedure and job_id can be arbitrary value. With arbitrary value i have either add or split partitions, whereas with list partitions i can only add them.
2. Do insert\select. But it is long time, i think.(a) according to your data, do you know exactly in what partition are you going to insert into?
(b) have you any locally partitioned indexes in this DELTAH table?
(c) have you any triggers or integrity constraints in your DELTAH
(d) are you deleting from DELTAH (or you will never delete from DELTAH)?
If the anwer to (a) is yes then you can user insert into DELTAH partition (the_exact_partition) select....
If the answer to (b) is yes and those indexes are not policing a primary key or a unique key then you can set the corresponding partition indexes into an unusable state and rebuild those
specific partitions after the load
If the answer to both (c) and (d) is no, then I would suggest finally to do
insert /*+ append */ into DELTAH partition (the_exact_partition)
select ...Hope his helps
Mohamed Houri -
How to transfer data between two tables
Hi friends i have got a new problem actually i'm a fresher in this SAP so i have a doubt in SAP Smart Forms the problem is like this
I have a data element common in both of the tables BSEG and KNA1 and the common data element is KUNNR so i want to transfer all the data in the table BSEG to KNA1 of the same data element(KUNNR) in which there is data regarding customer information so i want to transfer the single customer data into another table KNA1 and i want to display the data in the output plz any one can help me.hi.
the below metjhod u can adopt.
1)
select data from 1st zable to internal table.
2)insert data from internal table to 2nd ztable simple.
data:begin of itab occurs 0.
include structure ztable_structure1.
data:end of itab.
select * from ztable into table itab.
modify ztable1 FROM TABLE itab.
3 ) ike that u can catch date from other table.
4) if any condition is there u can check it in the internal table.
Rgds
Anver
if hlped pls mark points -
How can i merge data between two tables and then insert to another table
Hi ,
Could pls help me,
I have two tables i need to merge this tables for a single column then need to insert the records to a third table
Ex-
Suppose emp, dept two tables , merge this two tables for empid then insert that value to emp_dept table.
I am using oracle 10g.
ThanksHi,
too many values comes from the select clause. I stated you have to match the columns from the emp_dept table to the columns in the select. In my example I return all the columns of emp and dept, but I think you have only one empid column, the select for example will give 2, one empid from emp and one empid from dept. So best is to match:
insert into emp_dept
(column1,column2,column3,empid)
select emp.column1, emp.column2, dept.column3, emp.empid
from emp, dept
where emp.empid = dept.empidAbove is an example how you can match, so the number of columns in the insert should match with the number of columns coming from the select.
But better for us to help you is give your definitions of emp, dept and emp_dept.
Herald ten Dam
htendam.wordpress.com
Maybe you are looking for
-
Service desk-Support Messages-"Reported by" not getting updated
Hi gurus, In a support message when we create message from satellite system. System is not captuing automatically the "Reported by" field.(BP number of the creator). Can you pl guide me, is there any missing setting to capture this "Reported By" fiel
-
Approved order with payment card - still appears in VKM3
I have the following scenario in ECC 5.0 - 1) We enter a sales order with payment card. For some reason, the authorization fails. The order goes on a credit block. 2) As a result, it appears in VCC1 and VKM3 lists. 3) Later, if the order is changed a
-
IPhone - Safari parent window focus problem
Hi All, I am having a peculiar problem with a website i am developing. The problem *only* happens on the iPhone(safari). My setup is as follows. 1) I have a CLIENT page - shows an initial page, and has a login button. 2) User clicks on the LOGIN butt
-
hi expects, i have take the attachment data from my offical mail and i have send to idoc in sap. i am using POP but it is taking all the mail .how cam i solve this problem..
-
Assigning Applications to Desktops
How do I assign "non-lion" 100% compatible applications to a Desktop - since Expose/Spaces is gone, I'm trying to assign VMWare to it's OWN desktop, and so far, I can assign Chrome to it's own Desktop by right clicking on it ("Assign to THIS Desktop"