This SQL query performs an in-depth analysis of shipment data, focusing on delivery times, customer behavior, and carrier performance. The query is structured in three main parts: two Common Table Expressions (CTEs) and a final SELECT statement.
This was inspired by a post by Mehtab Karta.
This query is designed to work with the fulfil data warehouse. Ensure you have access to this data warehouse before attempting to run the query.
The first CTE, customer_metrics
, calculates various metrics for each customer:
- Average delivery time (in days)
- Order count
- Total amount spent
- Whether the customer has made repeat purchases
It joins the shipments
and sales_orders
tables to gather this information. The WHERE clause ensures that only completed shipments with valid data are included in the analysis.
The second CTE, delivery_time_bins
, categorizes the average delivery times into bins:
- 0-3 days
- 4-5 days
- 6-7 days
- 8-10 days
- 10+ days
This categorization allows for easier analysis of delivery performance.
The final SELECT statement aggregates the data from the delivery_time_bins
CTE to provide insights on:
- Number of customers in each delivery time bin
- Average customer lifetime value (LTV) for each bin
- Repurchase rate for each bin
The results are grouped by delivery time bin, carrier, and carrier service, then ordered for easy interpretation.
This query can help answer questions such as:
- Which carriers and services have the fastest delivery times?
- How does delivery time affect customer lifetime value?
- Is there a correlation between delivery speed and likelihood of repurchase?
- Which carriers are handling the most customers?
By analyzing this data, the company can make informed decisions about which carriers to use, how to improve customer satisfaction, and potentially how to increase customer lifetime value and repurchase rates.
To create a custom report using this query in the fulfil data warehouse:
- Log in to your fulfil account and navigate to the Reports section.
- Click on "Create New Report" or "New Custom Report".
- In the query editor, paste the entire SQL query provided above.
- You may need to adjust table names or field names to match your specific data warehouse schema.
- Click "Run Query" to test the report and ensure it executes without errors.
- Once satisfied with the results, give your report a name and description.
- Set up any desired scheduling or sharing options for the report.
- Save the report.
Remember to review and update the report periodically to ensure it continues to meet your business needs and reflect any changes in your data structure or business processes.
Note: Depending on the size of your data warehouse and the complexity of the query, this report might take some time to run. Consider adding date range filters if you need to limit the data processed for performance reasons.