BIGQUERY SQL to compare two tables
- or -
Post a project like this1317
$25/hr
- Posted:
- Proposals: 2
- Remote
- #2931009
- Awarded
Description
Experience Level: Expert
Compare two tables then return rows that need to be deleted, updated, or inserted. And note which need to be done for each row.
This is what I have now:
CREATE OR REPLACE TABLE hm_connection.update_contacts AS
SELECT
CASE
WHEN a.contactid IS NULL AND b.contactid IS NOT NULL THEN "I"
WHEN a.contactid IS NOT NULL AND b.contactid IS NULL THEN "D"
ELSE "U"
END AS op,
IF(b.contactid IS NULL, a, b).*
FROM
`hm_connection.new_contacts` a
FULL OUTER JOIN `hm_connection.contacts` b
ON a.contactid = b.contactid
WHERE
a.contactid IS NULL OR
b.contactid IS NULL OR
FARM_FINGERPRINT(FORMAT("%T", a)) <>
FARM_FINGERPRINT(FORMAT("%T", b));
The problem is with IF(b.contactid IS NULL, a, b).*, because the contactid is a STRING. I've attached sample data. What I need is something that compares all value to see if there are any changes in any of the cells of data.
This is what I have now:
CREATE OR REPLACE TABLE hm_connection.update_contacts AS
SELECT
CASE
WHEN a.contactid IS NULL AND b.contactid IS NOT NULL THEN "I"
WHEN a.contactid IS NOT NULL AND b.contactid IS NULL THEN "D"
ELSE "U"
END AS op,
IF(b.contactid IS NULL, a, b).*
FROM
`hm_connection.new_contacts` a
FULL OUTER JOIN `hm_connection.contacts` b
ON a.contactid = b.contactid
WHERE
a.contactid IS NULL OR
b.contactid IS NULL OR
FARM_FINGERPRINT(FORMAT("%T", a)) <>
FARM_FINGERPRINT(FORMAT("%T", b));
The problem is with IF(b.contactid IS NULL, a, b).*, because the contactid is a STRING. I've attached sample data. What I need is something that compares all value to see if there are any changes in any of the cells of data.
Adam M.
100% (29)Projects Completed
21
Freelancers worked with
15
Projects awarded
71%
Last project
12 Oct 2022
United States
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
There are no clarification messages.
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies