INSERT IGNORE INTO beta_live.Customers
(cust_no, cust_name, aff_cust_no, aff_customer_id, aff_cust_name, addr_1, addr_2, addr_3, addr_4, addr_5, city, state, zip_postal, country, category, customercategory_id, incept_dt, currency)
SELECT
beta_history.Customers_latest.cust_no, beta_history.Customers_latest.cust_name, beta_history.Customers_latest.aff_cust_no, CustomersA.id aff_customer_id, beta_history.Customers_latest.aff_cust_name, beta_history.Customers_latest.addr_1, beta_history.Customers_latest.addr_2, beta_history.Customers_latest.addr_3, beta_history.Customers_latest.addr_4, beta_history.Customers_latest.addr_5, beta_history.Customers_latest.city, beta_history.Customers_latest.state, beta_history.Customers_latest.zip_postal, beta_history.Customers_latest.country, beta_history.Customers_latest.category, CustomerCategories.id customercategory_id, beta_history.Customers_latest.incept_dt, beta_history.Customers_latest.currency FROM beta_history.Customers_latest
LEFT OUTER JOIN beta_live.Customers USING (cust_no)
LEFT OUTER JOIN beta_live.Customers CustomersA ON (CustomersA.cust_no = beta_history.Customers_latest.aff_cust_no)
LEFT OUTER JOIN beta_live.CustomerCategories CustomerCategories ON (CustomerCategories.cust_category = beta_history.Customers_latest.category)
 ON DUPLICATE KEY UPDATE cust_no=VALUES(cust_no), cust_name=VALUES(cust_name), aff_cust_no=VALUES(aff_cust_no), aff_customer_id=VALUES(aff_customer_id), aff_cust_name=VALUES(aff_cust_name), addr_1=VALUES(addr_1), addr_2=VALUES(addr_2), addr_3=VALUES(addr_3), addr_4=VALUES(addr_4), addr_5=VALUES(addr_5), city=VALUES(city), state=VALUES(state), zip_postal=VALUES(zip_postal), country=VALUES(country), category=VALUES(category), customercategory_id=VALUES(customercategory_id), incept_dt=VALUES(incept_dt), currency=VALUES(currency), deleted_at=NULL;

UPDATE beta_live.Customers
LEFT OUTER JOIN beta_history.Customers_latest
ON (beta_live.Customers.cust_no = beta_history.Customers_latest.cust_no)
SET beta_live.Customers.deleted_at = NOW()

WHERE beta_history.Customers_latest.record_hash IS NULL
AND beta_live.Customers.deleted_at IS NULL;



INSERT IGNORE INTO beta_live.Customers (cust_no, cust_name, aff_cust_no, aff_customer_id, aff_cust_name, addr_1, addr_2, addr_3, addr_4, addr_5, city, state, zip_postal, country, category, customercategory_id, incept_dt, currency) SELECT beta_history.Customers_latest.cust_no, beta_history.Customers_latest.cust_name, beta_history.Customers_latest.aff_cust_no, CustomersA.id aff_customer_id, beta_history.Customers_latest.aff_cust_name, beta_history.Customers_latest.addr_1, beta_history.Customers_latest.addr_2, beta_history.Customers_latest.addr_3, beta_history.Customers_latest.addr_4, beta_history.Customers_latest.addr_5, beta_history.Customers_latest.city, beta_history.Customers_latest.state, beta_history.Customers_latest.zip_postal, beta_history.Customers_latest.country, beta_history.Customers_latest.category, CustomerCategories.id customercategory_id, beta_history.Customers_latest.incept_dt, beta_history.Customers_latest.currency FROM beta_history.Customers_latest LEFT OUTER JOIN beta_live.Customers USING (cust_no) LEFT OUTER JOIN beta_live.Customers CustomersA ON (CustomersA.cust_no = beta_history.Customers_latest.aff_cust_no) LEFT OUTER JOIN beta_live.CustomerCategories CustomerCategories ON (CustomerCategories.cust_category = beta_history.Customers_latest.category) ON DUPLICATE KEY UPDATE cust_no=VALUES(cust_no), cust_name=VALUES(cust_name), aff_cust_no=VALUES(aff_cust_no), aff_customer_id=VALUES(aff_customer_id), aff_cust_name=VALUES(aff_cust_name), addr_1=VALUES(addr_1), addr_2=VALUES(addr_2), addr_3=VALUES(addr_3), addr_4=VALUES(addr_4), addr_5=VALUES(addr_5), city=VALUES(city), state=VALUES(state), zip_postal=VALUES(zip_postal), country=VALUES(country), category=VALUES(category), customercategory_id=VALUES(customercategory_id), incept_dt=VALUES(incept_dt), currency=VALUES(currency), deleted_at=NULL;

UPDATE beta_live.Customers LEFT OUTER JOIN beta_history.Customers_latest ON (beta_live.Customers.cust_no = beta_history.Customers_latest.cust_no) SET beta_live.Customers.deleted_at = NOW() WHERE beta_history.Customers_latest.record_hash IS NULL AND beta_live.Customers.deleted_at IS NULL;
