Source Database Table
drp.fulfillment_cost
Script that Populates Database Table
4140_CST_BAS_fulfillment_cost.sql
Script Description and Logic
Pulls fulfillment id, fulfillment cost, shipping cost, number of order lines and total items at the fulfillment level. Fulfillments from all integrations are included in the primary source table, uos.fulfillments, and reported by unique fulfillment id, shop id and integration id combination. From the uos.fulfillments and uos.order_item_fulfillments table, a lookup is made to return a list of fulfillments that have been updated in the last seven days. This list is then inserted into uos_fulfillments_to_update. Fulfillments and order_item_fulfillments are then joined back to uos_fulfillments_to_update, the number of fulfilled items is summed, the cost is determined at the item level. The unique combinations of fulfillment id, shop id, fulfillment cost, shipping cost and number of items are then placed in a staging table in preparation for incremental functionality. Incremental functionality is included such that only the records that require an update or are new get inserted into the final drp.fulfillment_cost table. The code looks back 7 days from last_load_dt to get that list of orders and then holds them in a staging table. The records in staging are compared to those in the final table. Any fulfillments that match are removed from the final table and updated by way of insertion from staging. The records that appear in staging, but not in the final table are inserted into drp.fulfillment_cost.
Source Tables Used In Script
Schema | Table (or Derived Table) Name | Table Type | Purpose |
uos | fulfillments | database | Primary source |
uos | order_item_fulfillments | database | Primary source |
drp_staging | uos_fulfillments_to_update | database | Lookup |
drp_staging | fulfillment_cost | database | Pre-insert |
N/A | last_sync_date | derived | Obtain last sync date for each record |
N/A | fulfillments_since_last_sync | derived | Determine which fulfillments or orders have been updated in the last seven days |
SQL Flow
Calculated and Derived Fields
Target Column | Target Column Data Type | Source schema.table | Source Column | Transformation/Logic |
num_order_lines | INT | uos.order_item_fulfillments | order_line_id | COUNT(unique order line) per unique fulfillment id and shop_id |
num_items | INT | uos.order_item_fulfillments | ordered_quantity, remaining_to_fulfill | SUM(ordered_quanity - reminaing_to_fulfill) per unique fulfillment id and shop_id |
All content © Daasity 2021. Do not copy, share or distribute.