Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
sms_query_to_rebuild_contacts_from_order_information [2019/01/27 11:31] 127.0.0.1 external edit |
sms_query_to_rebuild_contacts_from_order_information [2021/11/17 17:26] (current) admin [Explanation of SQL] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== | ====== | ||
| - | |||
| - | |||
| ===== Explanation of SQL ===== | ===== Explanation of SQL ===== | ||
| - | + | This SQL will insert any missing contacts in the " | |
| - | + | ||
| - | This SQL will rebuild | + | |
| Line 14: | Line 9: | ||
| **High**. Data is modified in this query. Do not run this except under the direction of a Cyrious Technical Support staff member. Doing otherwise may result in lost or contaminated data. All data modifications done through direct SQL **are permanent and non-reversable**. | **High**. Data is modified in this query. Do not run this except under the direction of a Cyrious Technical Support staff member. Doing otherwise may result in lost or contaminated data. All data modifications done through direct SQL **are permanent and non-reversable**. | ||
| + | ===== SQL ===== | ||
| + | <code sql> | ||
| + | //Insert Missing Contacts from Orders | ||
| + | SELECT ContactID, MAX(OrderID) AS OrderID | ||
| + | FROM "Order Database" | ||
| + | WHERE ContactID IS NOT NULL | ||
| + | AND OrderDate> | ||
| + | AND ContactID NOT IN (SELECT ContactID FROM " | ||
| + | GROUP BY ContactID | ||
| + | ORDER BY ContactID | ||
| + | ; ContactList | ||
| + | SELECT ContactID, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | TRUE AS PrimaryContact, | ||
| + | TRUE AS ShippingContact, | ||
| + | TRUE AS APContact, | ||
| + | TRUE AS InvoicingContact, | ||
| + | "" | ||
| + | | ||
| + | | ||
| + | | ||
| + | TRUE AS IsActive, | ||
| + | '' | ||
| + | ' | ||
| + | FROM "Order Database" | ||
| + | WHERE OrderID IN (SELECT OrderID FROM ContactList) | ||
| + | ORDER BY ContactID | ||
| + | ; LatestOrderContacts FALSE | ||
| - | ===== SQL ===== | + | INSERT INTO " |
| + | SELECT * FROM LatestOrderContacts | ||
| + | ; | ||
| + | //Insert Missing Contacts from Estimates | ||
| + | SELECT ContactID, MAX(OrderID) AS OrderID | ||
| + | FROM " | ||
| + | WHERE ContactID IS NOT NULL | ||
| + | AND EstimateDate> | ||
| + | AND ContactID NOT IN (SELECT ContactID FROM " | ||
| + | GROUP BY ContactID | ||
| + | ORDER BY ContactID | ||
| + | ; ContactList | ||
| + | SELECT ContactID, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | TRUE AS PrimaryContact, | ||
| + | TRUE AS ShippingContact, | ||
| + | TRUE AS APContact, | ||
| + | TRUE AS InvoicingContact, | ||
| + | "" | ||
| + | | ||
| + | | ||
| + | | ||
| + | TRUE AS IsActive, | ||
| + | '' | ||
| + | ' | ||
| + | FROM " | ||
| + | WHERE OrderID IN (SELECT OrderID FROM ContactList) | ||
| + | ORDER BY ContactID | ||
| + | ; LatestOrderContacts FALSE | ||
| - | <code sql> | + | INSERT INTO "Contact |
| - | select ContactID, max(OrderID) as OrderID | + | SELECT * FROM LatestOrderContacts |
| - | from "Order Database" | + | |
| - | where ContactID is not null | + | |
| - | group by ContactID | + | |
| - | order by ContactID | + | |
| - | ; ContactList | + | |
| - | select ContactID, | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | True as PrimaryContact, | + | |
| - | True as ShippingContact, | + | |
| - | True as APContact, | + | |
| - | True as InvoicingContact, | + | |
| - | "" | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | True as IsActive | + | |
| - | from "Order Database" | + | |
| - | where OrderID in (select OrderID from ContactList) | + | |
| - | order by ContactID | + | |
| - | ; LatestOrderContacts | + | |
| - | delete from " | + | |
| - | where ContactID in (Select ContactID from ContactList) | + | |
| ; | ; | ||
| - | insert into " | + | |
| - | select * from LatestOrderContacts | + | |
| - | ; | + | |
| - | // Now pull what is left from the Estimates | + | |
| - | select ContactID, max(OrderID) as OrderID | + | |
| - | from "Order Database" | + | |
| - | where ContactID is not null and ContactID is not in (Select ContactID from " | + | |
| - | group by ContactID | + | |
| - | order by ContactID | + | |
| - | ; ContactList | + | |
| - | select ContactID, | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | True as PrimaryContact, | + | |
| - | True as ShippingContact, | + | |
| - | True as APContact, | + | |
| - | True as InvoicingContact, | + | |
| - | "" | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | True as IsActive | + | |
| - | from "Order Database" | + | |
| - | where OrderID in (select OrderID from ContactList) | + | |
| - | order by ContactID | + | |
| - | ; LatestEstimateContacts False | + | |
| - | insert into " | + | |
| - | select * from LatestEstimateContacts | + | |
| - | ; | + | |
| - | update " | + | |
| - | set PrimaryContact = False | + | |
| - | where ContactID not in (select PrimaryContactID from " | + | |
| - | ; | + | |
| - | update " | + | |
| - | set APContact = False | + | |
| - | where ContactID not in (select APContactID from " | + | |
| - | ; | + | |
| - | select * from " | + | |
| </ | </ | ||
| - | |||
| - | |||
| ===== Version Information ===== | ===== Version Information ===== | ||
| - | * Entered : 03/2010 | ||
| - | * Version : 8.6+ | ||
| - | |||
| + | * Modified: 11/2021 | ||
| + | * Version : 8.91 | ||