Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE feed_tbl
- (
- code varchar(15),
- name varchar(40),
- status char(1),
- update char(1)
- CONSTRAINT feed_tbl_PK PRIMARY KEY (code)
- CREATE TABLE data_tbl
- (
- code varchar(15),
- name varchar(40),
- status char(1),
- update char(1)
- CONSTRAINT data_tbl_PK PRIMARY KEY (code)
- CREATE VIEW delta_vw AS (
- SELECT feed_tbl.code, feed_tbl.name, feed_tbl.status, feed_tbl.update, 0 as diff_type
- FROM feed_tbl LEFT OUTER JOIN
- data_tbl ON feed_tbl.code = data_tbl.code
- WHERE (data_tbl.code IS NULL)
- UNION
- SELECT feed_tbl.code, feed_tbl.name, feed_tbl.status, feed_tbl.update, 1 as diff_type
- FROM data_tbl RIGHT OUTER JOIN
- feed_tbl ON data_tbl.code = feed_tbl.code
- where (feed_tbl.name <> data_tbl.name) OR
- (data_tbl.status <> feed_tbl.status) OR
- (data_tbl.update <> feed_tbl.update)
- UNION
- SELECT data_tbl.code, data_tbl.name, data_tbl.status, data_tbl.update, 2 as diff_type
- FROM feed_tbl LEFT OUTER JOIN
- data_tbl ON data_tbl.code = feed_tbl.code
- WHERE (feed_tbl.code IS NULL)
- )
- select dt.*
- from
- data_tbl dt
- ,(
- select code
- from
- (
- select * from feed_tbl
- union
- select * from data_tbl
- )
- group by code
- having count(*) > 1
- ) diffs --"diffs" will return all differences *except* those in the primary key itself
- where diffs.code = dt.code
- union --plus the ones that are only in feed, but not in data
- select * from feed_tbl ft where not exists(select code from data_tbl dt where dt.code = ft.code)
- union --plus the ones that are only in data, but not in feed
- select * from data_tbl dt where not exists(select code from feed_tbl ft where ft.code = dt.code)
Add Comment
Please, Sign In to add comment