SQL query to find differences (changes) between tables (from one table) where field names are different
Hi All,
I am looking to create a view which returns new or modified data (differences) based on a comparison between two tables.
The EMP_SOURCE table stores all employee data including duplicate staff numbers (STAFFNO):
CREATE TABLE [dbo].[EMP_SOURCE](
[FULLNAME] [varchar](255) NULL,
[JOBTITLE] [varchar](255) NULL,
[LOCATION] [varchar](255) NULL,
[COUNTRY] [varchar](255) NULL,
[STAFFNO] [varchar](255) NULL
) ON [PRIMARY]
GO
The EMP table stores unique staff numbers. This is the table used by the application.
CREATE TABLE [dbo].[EMP](
[EMP_ID] [int] NOT NULL,
[EMP_NAME] [varchar](255) NULL,
[EMP_TITLE] [varchar](255) NULL,
[EMP_OFFICE] [varchar](255) NULL,
[EMP_COUNTRY] [varchar](255) NULL,
[EMP_NUMBER] [varchar](255) NULL,
CONSTRAINT [PK_EMP] PRIMARY KEY CLUSTERED
[EMP_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
We are looking to migrate data from EMP_SOURCE to EMP but only records which are new in EMP_SOURCE and do not exist in EMP or records which are different in EMP_SOURCE from EMP.
EMP_SOURCE:
FULLNAME
JOBTITLE
LOCATION
COUNTRY
STAFFNO
John Smith
Manager
London
UK
1087
Beth King
Analyst
New York
USA
2095
Karl Bent
Manager
Chicago
USA
1106
Beth King
Junior
Washington
USA
2095
Harry Kline
Consultant
Manchester
UK
2341
EMP:
EMP_ID
EMP_NAME
EMP_TITLE
EMP_OFFICE
EMP_COUNTRY
EMP_NUMBER
1
John Smith
Manager
London
UK
1087
2
Beth King
Analyst
New York
USA
2095
3
Karl Bent
Manager
Washington
USA
1106
Based on the above comparison, EMP_SOURCE table has the following differences:
FULLNAME
JOBTITLE
LOCATION
COUNTRY
STAFFNO
Harry Kline
Consultant
Manchester
UK
2341
Karl Bent
Manager
Chicago
USA
1106
Differences in red. Beth King should be completely ignored because of duplicate staff numbers (EMP_NUMBER).
Any help to create a view which returns only the differences from EMP_SOURCE would be appreciated.
INSERT INTO EMP (EMP_ID, EMP_NAME, EMP_TITLE, EMP_OFFICE, EMP_COUNTRY, EMP_NUMBER)
VALUES (1, 'John Smith', 'Manager', 'London', 'UK', '1087');
INSERT INTO EMP (EMP_ID, EMP_NAME, EMP_TITLE, EMP_OFFICE, EMP_COUNTRY, EMP_NUMBER)
VALUES (2, 'Beth King', 'Analyst', 'New York', 'USA', '2095');
INSERT INTO EMP (EMP_ID, EMP_NAME, EMP_TITLE, EMP_OFFICE, EMP_COUNTRY, EMP_NUMBER)
VALUES (3, 'Karl Bent', 'Manager', 'Washington', 'USA', '1106');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('John Smith', 'Manager', 'London', 'UK', '1087');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('Beth King', 'Analyst', 'New York', 'USA', '2095');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('Karl Bent', 'Manager', 'Chicago', 'USA', '1106');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('Beth King', 'Junior', 'Washington', 'USA', '2095');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO) VALUES ('Harry Kline', 'Consultant', 'Manchester', 'UK', '2341');
HI Manc !
You may use the below code to get your desired output;
CREATE TABLE [dbo].[EMP_SOURCE](
[FULLNAME] [varchar](255) NULL,
[JOBTITLE] [varchar](255) NULL,
[LOCATION] [varchar](255) NULL,
[COUNTRY] [varchar](255) NULL,
[STAFFNO] [varchar](255) NULL
GO
CREATE TABLE [dbo].[EMP](
[EMP_ID] [int] NOT NULL,
[EMP_NAME] [varchar](255) NULL,
[EMP_TITLE] [varchar](255) NULL,
[EMP_OFFICE] [varchar](255) NULL,
[EMP_COUNTRY] [varchar](255) NULL,
[EMP_NUMBER] [varchar](255) NULL
INSERT INTO EMP (EMP_ID, EMP_NAME, EMP_TITLE, EMP_OFFICE, EMP_COUNTRY, EMP_NUMBER)
VALUES (1, 'John Smith', 'Manager', 'London', 'UK', '1087');
INSERT INTO EMP (EMP_ID, EMP_NAME, EMP_TITLE, EMP_OFFICE, EMP_COUNTRY, EMP_NUMBER)
VALUES (2, 'Beth King', 'Analyst', 'New York', 'USA', '2095');
INSERT INTO EMP (EMP_ID, EMP_NAME, EMP_TITLE, EMP_OFFICE, EMP_COUNTRY, EMP_NUMBER)
VALUES (3, 'Karl Bent', 'Manager', 'Washington', 'USA', '1106');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('John Smith', 'Manager', 'London', 'UK', '1087');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('Beth King', 'Analyst', 'New York', 'USA', '2095');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('Karl Bent', 'Manager', 'Chicago', 'USA', '1106');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO)
VALUES ('Beth King', 'Junior', 'Washington', 'USA', '2095');
INSERT INTO EMP_SOURCE (FULLNAME, JOBTITLE, LOCATION, COUNTRY, STAFFNO) VALUES ('Harry Kline', 'Consultant', 'Manchester', 'UK', '2341');
SELECT FULLNAME,JOBTITLE,LOCATION,COUNTRY,STAFFNO FROM EMP_SOURCE
EXCEPT
SELECT EMP_NAME,EMP_TITLE,EMP_OFFICE,EMP_COUNTRY,EMP_NUMBER FROM Emp
Please let me know if this doesn’t work for you. Hope I have answered you correctly.
Thanks,
Hasham
Similar Messages
-
SQL Query to find the Delta between 2 rows
Can the below be possible? If so can you help me writing aSQL Query to do so…
I have data in spreadsheet which is pulled off from the database (data from more than one table with joins); send it to the different teams. They will check the data n update the spreadsheet if necessary and send it back to me.
I have to find the changes and update the database from the provided spreadsheet accordingly. Changes can be on different columns on each set of row.
Example:
DataFrom
ServerName
Branch_Name
Application_Name
Server Status
Application_Status
App_Environment
Tier
SQL Query
abcdef
app
adp
Deployed
Deployed
Production
silver
Excel
abcdef
app
adp
Deployed
Deployed
Development
Bronze
DataFrom
ServerName
Branch_Name
Application_Name
Server Status
Application_Status
App_Environment
Tier
SQL Query
Hijkl
app
adp
Deployed
Deployed
Production
Gold
Excel
Hijkl
app
Dep
Deployed
Deployed
Production
Gold
DataFrom
ServerName
Branch_Name
Application_Name
Server Status
Application_Status
App_Environment
Tier
SQL Query
Xzy
app
Dep
Deployed
Deployed
Production
Silver
Excel
Xzy
App
Dep
Deployed
Deployed
Development
Silver
Above scenario is an example what I am look to do with sql script? Opinions/queries accepted…
There are 1200+ rows to compare it manually which is a pain.
Thanks.Columns are different, when the contain multiple distinct values.
SELECT COUNT(DISTINCT Name) ,
COUNT(DISTINCT GroupName) ,
COUNT(*)
FROM HumanResources.Department;
Without a concise and complete example (table DDL and sample data insert statements), it's hard to tell what the correct solution could be..
DECLARE @Sample TABLE ( SetID INT, ServerID INT, ApplicationID INT );
INSERT INTO @Sample
VALUES ( 1, 1, 1 ),
( 1, 1, 1 ),
( 2, 1, 1 ),
( 2, 1, 2 ),
( 3, 1, 1 ),
( 3, 2, 1 );
WITH Evaluate AS
SELECT SetID,
COUNT(DISTINCT ServerID) AS Servers,
COUNT(DISTINCT ApplicationID) AS Applications
FROM @Sample
GROUP BY SetID
SELECT S.*,
CASE WHEN E.Servers != 1 THEN 1 ELSE 0 END AS ServersDifferent,
CASE WHEN E.Applications != 1 THEN 1 ELSE 0 END AS ApplicationsDifferent
FROM @Sample S
INNER JOIN Evaluate E ON S.SetID = E.SetID
ORDER BY S.SetID; -
SQL Query issue find difference in records
Hi All,
I am using oracle 10g. I need urgent help in finding difference in records based on date:
I have table sales as below:
salesman SALES_COUNT DATE
JOHN 20 04/01/2012
DENNY 15 04/01/2012
JOHN 30 04/02/2012
DENNY 30 04/02/2012
JOHN 45 04/03/2012
DENNY 50 04/03/2012
SALES_COUNT is increasing for sales man with date. Its like cumulative count. John has total sales from 04/01/2012 to 04/03/2012 is 50 and same case with Denny. This SALES_COUNT will keep on increasing with dates as sales keep adding in the table for each salesman.
But i want to have seprate counts for each salesman.
for e.g: SALES_COUNT JOHN on 04/02/2012 is 30-20 =10
SALES_COUNT JOHN on 04/03/2012 is 45-30 =15
SALES_COUNT DENNY on 04/02/2012 is 30-15 =15
SALES_COUNT JOHN on 04/03/2012 is 50-30 =20
Please help me with this scenario and let me know if need more information. I will greatly appreciate your help.
Thanks.Does this give you what you want?
with t as (
select 'JOHN' salesman, 20 sales_count, to_date('04/01/2012', 'mm/dd/yyyy') sale_date from dual
union all
select 'DENNY' salesman, 15 sales_count, to_date('04/01/2012', 'mm/dd/yyyy') sale_date from dual
union all
select 'JOHN' salesman, 30 sales_count, to_date('04/02/2012', 'mm/dd/yyyy') sale_date from dual
union all
select 'DENNY' salesman, 30 sales_count, to_date('04/02/2012', 'mm/dd/yyyy') sale_date from dual
union all
select 'JOHN' salesman, 45 sales_count, to_date('04/03/2012', 'mm/dd/yyyy') sale_date from dual
union all
select 'DENNY' salesman, 50 sales_count, to_date('04/03/2012', 'mm/dd/yyyy') sale_date from dual
select salesman,
sales_count sales_todate,
sale_date,
sales_count - lag(sales_count, 1, 0) over (partition by salesman order by sale_date) daily_sales
from t
SALESMAN,SALES_TODATE,SALE_DATE,DAILY_SALES
DENNY,15,4/1/2012,15
DENNY,30,4/2/2012,15
DENNY,50,4/3/2012,20
JOHN,20,4/1/2012,20
JOHN,30,4/2/2012,10
JOHN,45,4/3/2012,15
-
Sql query to calculate total credit having more than one tables
i have these 9 tables and want to calculate totalcredit from these tables alse want to
See more: SQL-server-2008R2
i have these 9 tables and want to calculate totalcredit from these tables alse want to calculate totaldebit of all account no in which each accountno will show its own total credit and total debit
parties
purchase
purchase body
purchase return
purchase return body
sale
sale body
sale return
sale return bodyIf you want to suggest you accurate solution, please post CREATE TABLE+ INSERT INTO + sample data + DESIRED RESULT
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 -
To make three tables from one table
Hi,
I have created a random table from a big table and now i want to make three tables of same numbers, means each table should contain 1000 records, as the table has random idx column, so i cant divide directly on basis of idx. in addition, i cant create three seperate tbles from main table, as i dont want any duplicate data in those three tables, whcih may occur if i will create each table individually.
Best regards,Table 1.
create table emp1 as
select empno,ename,sal--,................
from(select e.*,row_number() over(order by rowid) rn,count(*) over(order by null) cnt
from emp e)
where rn < cnt/3
Table 2.
create table emp1 as
select empno,ename,sal--,................
from(select e.*,row_number() over(order by rowid) rn,count(*) over(order by null) cnt
from emp e)
where rn between cnt/3 and cnt*2/3
Table 3.
create table emp1 as
select empno,ename,sal--,................
from(select e.*,row_number() over(order by rowid) rn,count(*) over(order by null) cnt
from emp e)
where rn > cnt*2/3 -
Passing value to bind variable of another table from one table
hi,
I have a multi select table. When one row is selected from this table (no button is clicked, only selection is done), an attribute from that selected row (say userid) should be passed to the bind variable of another table and the corresponding details of that particular userid should be displayed in the other table. When more than one row is selected, the other table should display no rows.
My main problem is what code has to be written to pass value to bind variable and where it should be written.
Please give me a detailed explaination as soon as possible.
Thank you.Sorry, didnot add this. The table is multi select table.
-
Move a table from one table space to another tablespace and different table
Hi,
I have a 60gb table nearly 200 million records in it and the table has range partition.
inorder to archive this table we have created 3 different tables and 3 different tablespaces for the tables.
I want to move certain partitions to tableA and certain partitions to tableB and on to tableC and on the top i'm creating a view to access these 3 tables.
Is there any technique to move the partition wise data to different tables. is there any new technique in 11g database?
The database is 11gr1
linux rhel5 x-86-64
Edited by: user8894072 on Oct 12, 2010 2:03 PMif i do that like each partition has some millions of records. and the undotablespace is filling out and also the temp table space is filling out. The performance is very very poor if i use the create statement.
Insert INTO TableA (select * FROM <BASETABLE> where pay_end_dt between '01-Jan-1999' and '31-Dec-2005')
the above statement is also degrading the performance. I just need the synatx to move the partioned wise data to new table. -
Query to find all relationships between tables
Please help me wit the following
Query to find all relationships between tables
SAMPLE OUTPUT:
PRIMTAB PRIMCOL FOREIGNTAB FOREIGN KEY
DEPT DEPTNO EMP DEPTNO
Return all records in the database.
PLEASE HELPSET LINESIZE 150
COLUMN primcol FORMAT A30
COLUMN foreigncol FORMAT A30
SELECT uc1.table_name AS primtab,
ucc1.column_name AS primcol,
uc2.table_name AS foreigntab,
ucc2.column_name AS foreigncol
FROM user_constraints uc1,
user_constraints uc2,
user_cons_columns ucc1,
user_cons_columns ucc2
WHERE uc1.constraint_name = uc2.r_constraint_name
AND uc1.constraint_name = ucc1.constraint_name
AND uc2.constraint_name = ucc2.constraint_name
AND ucc1.position = ucc2.position
ORDER BY uc1.table_name,
ucc1.position
/ -
SQL Query to find menus and submenus attached to responsibility
Hi,
I am looking for help to find out a sql query to pull out the list of all the menu's associated with each of the responsibilities assigned to users. Please let me know any SQL query to find out menus attached for responsibilities assigned to users.835129 wrote:
I was asked by my lead to list out responsibilities and attached menus and I was asked to submit the output from production. In the metalink note provided by you it was asked to create table collecting all menu id's and I cannot create any tables in production. Apart from this there were 1000's of users with different responsibilities and different menus. I cannot collect all of the users menu ids.
I just want to list out responsibilities and attached menus. Is that something you can helpout withhttps://forums.oracle.com/forums/search.jspa?threadID=&q=fnd_responsibility_vl+AND+fnd_menu&objID=c3&dateRange=all&userID=&numResults=15&rankBy=10001
https://forums.oracle.com/forums/search.jspa?threadID=&q=fnd_responsibility_tl+AND+FND_MENU_ENTRIES_TL&objID=c3&dateRange=all&userID=&numResults=15&rankBy=10001
Please search the forum for these tables/views and you should get many hits:
FND_MENU_ENTRIES_TL
FND_MENU_ENTRIES_VL
FND_RESPONSIBILITY_TL
FND_RESPONSIBILITY_VL
Thanks,
Hussein -
SQL Query to Find out User has what all resources provisioned !
Hi Guys ,
Does any one have a SQL query to find out what resources are provisioned to a particular user ?
Thanks
SurenHi,
Hope this will help you.
SELECT distinct usr_login as "IdM User ID",
usr_employeeID as "Employee ID",
usr.USR_FIRST_NAME as "First Name",
usr.USR_LAST_NAME as "Last Name",
usr_status
as "User Status",
USR_EMP_TYPE as "Employee Type",
obj.obj_name as "Application Resource",
ost_status as "Application Resource Status",
FROM ost,oiu,obj,usr,obi
WHERE oiu.ost_key = ost.ost_key AND obj.obj_key = obi.obj_key AND oiu.usr_key = usr.usr_key
AND ost_status in ('Provisioned','Revoked','Disabled', 'Provisioning')
AND oiu.obi_key=obi.obi_key
AND usr_EmployeeID like '11111'
This query will provide all the resources to which the user is linked with and the resource status is in 'Provisioned','Revoked','Disabled', 'Provisioning' status for a particular employeed ID, I am not completely sure whether I have given the Employee ID column from USR table as correct or not. Verify once and query the DB -
SQL query to find access database files?
Odd request, but does anyone have a SQL query to find files? Specifically, I am looking for machines that have access databases on them. .mdb & .accdb
Have you enabled inventory for them?
Garth Jones | My blogs: Enhansoft and
Old Blog site | Twitter:
@GarthMJ
i was trying to figure out where that option was but couldn't find it under hierarchy settings. perhaps it's been changed since 2012 R2? can you help point it out? -
Sql query to find the balances for a customer account wise.
Hi,
Could someone help me with the sql query to find the balances for each customer account wise. This is need to generate the report.
presently we are using this query, but the output doesnot return the expected result.
SELECT sum(nvl(ps.acctd_amount_due_remaining,0)) "Balance"
FROM ra_cust_trx_line_gl_dist_all gld,
gl_code_combinations c,
ar_payment_schedules_all ps,
RA_CUSTOMER_TRX_ALL rat,
ra_customers rc
WHERE c.CHART_OF_ACCOUNTS_ID = 101
and gld.code_combination_id = c.code_combination_id
and rat.CUSTOMER_TRX_ID=gld.CUSTOMER_TRX_ID
and rat.CUSTOMER_TRX_ID=ps.CUSTOMER_TRX_ID
and ps.customer_id=rc.customer_id
and ps.status='OP'
and ps.gl_date <= :PDATE
and ps.org_id=:PORGID
and ps.class in ('GUAR','INV','DM','DEP')
and c.SEGMENT4=:Account_id
and ps.customer_id=:Customer_id
Thanks in advance.
Kalyan.Can someone help us with this.
-
Can anybody provide the SQL query to find the files uploaded in a particular folder?
Hi All,
Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
Any help is greatly appreciated.
With best regards,
NevinNevin,
Be great if we could know the version of Portal. For Rel. 1, here's the query
select id,masterthingid from wwv_things
where siteid = &site
and cornerid = &corner
&site - Content Area id
&corner - Folder id
if you don't know the folder id, use
select id from wwv_corners where siteid = &site
and name = &folder
Hope this helps. I have run into this situation before. Usually, the culprits were
one of the following:
1. Junk Characters in description of item (caused due to Copy-Paste)
2. Special Characters in the File name
Hi All,
Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
Any help is greatly appreciated.
With best regards,
Nevin -
Sql query to find all contacts for an account
I wonder if someone wrote an sql query to find all contacts for an account number in Oracle customer master. We are on EBS 11.5.10.
I am also looking for sql query to find all ship to addresses for an account number.
Thanks.Can you also post the query for people who read this post and are also looking for an answer?
Regards,
Johan Louwers. -
SQl query to find incompatibility defined
If two programs are defined as incompatible with one another, the data these programs cannot access simultaneously must also be identified.
This can be found from concurrent program->define from front end ebs login, but is there any SQL query to find this for a particular program or request set??
Thanks in advance.Please search the forum before posting similar questions.
Concurrent-Conflict
Re: Concurrent-Conflict
Thanks,
Hussein
Maybe you are looking for
-
Problem with mac osx 10.6.4 and audio firewire device
ok i use an edirol fa-66 , the problem is a random one , sometimes when i play music with itunes , logic or even the preview mp3 feature , the sound go downsampled and then stops, the only way to return is turn off and turn on the audio interface. I
-
Starting of oracle application server from the console.
D:\>cd or* D:\Oracle>cd j2* D:\Oracle\j2ee>cd home D:\Oracle\j2ee\home>java -jar oc4j.jar java.lang.NoClassDefFoundError: com/sun/tools/javac/Main Exception in thread "main" 2006-08-08 16:24:50.812 ERROR J2EE EJB3027 [system] A n error occured deploy
-
Linking Access tables, creating a query with using both Access and Oracle
Hello, I am using 3.0.04.34 version Oracle Developer. I am supposed to create a script/procedure to use both Access tables and oracle tables together. There is an option in developer to copy the access tables into oracle. But it doesn't help me. Beca
-
Good morning, after a leave request is approved, we want to make a calendar entry in outlook. We find the class CL_APPOINTMENT. On the whole it works, but we are not able the change the "show as" property. The appointment should appear as absent in t
-
Hi every ones: I'm develop some applications using PHP and Oracle and php give me a lot of advantage. in bouth way of access to a database, OCI and mod plsql. How can i say to Oracle THANKS FOR TAKE SERIOUS PHP TECNOLOGY!!! I work actually for a comp