Find the Average Delivery Time by Product Category Query Using MS SQL Server?
To find the average delivery time by product category using Microsoft SQL Server, you'll need to:
- Calculate the delivery time for each order by finding the difference between the delivery date and the order date.
- Aggregate the average delivery time for each product category.
Assuming you have the following tables:
Orderstable with columns:OrderID,OrderDate,DeliveryDate.OrderItemstable with columns:OrderID,ProductID,Quantity.Productstable with columns:ProductID,CategoryID.Categoriestable with columns:CategoryID,CategoryName.
Here's a step-by-step SQL query to calculate the average delivery time by product category:
SQL Query
-- Find the average delivery time by product category
WITH DeliveryTimes AS (
SELECT
oi.ProductID,
DATEDIFF(DAY, o.OrderDate, o.DeliveryDate) AS DeliveryTimeDays
FROM
Orders o
JOIN OrderItems oi ON o.OrderID = oi.OrderID
WHERE
o.DeliveryDate IS NOT NULL AND o.OrderDate IS NOT NULL
),
ProductCategories AS (
SELECT
p.ProductID,
c.CategoryID
FROM
Products p
JOIN Categories c ON p.CategoryID = c.CategoryID
),
CategoryDeliveryTimes AS (
SELECT
pc.CategoryID,
AVG(dt.DeliveryTimeDays) AS AverageDeliveryTime
FROM
DeliveryTimes dt
JOIN ProductCategories pc ON dt.ProductID = pc.ProductID
GROUP BY
pc.CategoryID
)
SELECT
c.CategoryName,
cdt.AverageDeliveryTime
FROM
CategoryDeliveryTimes cdt
JOIN Categories c ON cdt.CategoryID = c.CategoryID
ORDER BY
cdt.AverageDeliveryTime;
Explanation
DeliveryTimesCTE:DATEDIFF(DAY, o.OrderDate, o.DeliveryDate) AS DeliveryTimeDays: Computes the delivery time in days for each order.JOIN OrderItems oi ON o.OrderID = oi.OrderID: Joins withOrderItemsto get product information for each order.WHERE o.DeliveryDate IS NOT NULL AND o.OrderDate IS NOT NULL: Ensures that both delivery and order dates are present to calculate the delivery time.
ProductCategoriesCTE:JOIN Categories c ON p.CategoryID = c.CategoryID: JoinsProductswithCategoriesto get the category for each product.
CategoryDeliveryTimesCTE:AVG(dt.DeliveryTimeDays) AS AverageDeliveryTime: Calculates the average delivery time for each category.GROUP BY pc.CategoryID: Groups the results byCategoryIDto compute the average delivery time per category.
Main Query:
JOIN Categories c ON cdt.CategoryID = c.CategoryID: Joins with theCategoriestable to get the category names.ORDER BY cdt.AverageDeliveryTime: Orders the results by average delivery time for better readability.
Additional Considerations
- Handling Missing Data: Ensure that
OrderDateandDeliveryDateare properly populated. Handle cases where data might be missing or incorrect. - Indexes: Indexes on
OrderDate,DeliveryDate, andProductIDcan help improve query performance. - Data Quality: Verify that the
Orders,OrderItems,Products, andCategoriestables contain accurate and complete data.
Adjust column names and table structures according to your actual schema. This query calculates the average delivery time by product category effectively.
Post a Comment