northwind练习:28.计算每一笔订单的发货期(从订购到发货),运货期(从发货到到货)的时常,并按照发货期从长到短的顺序进行排序。
时间: 2024-12-12 11:30:19 浏览: 6
要在 Northwind 数据库练习中完成这个任务,你需要首先确定每个订单的详细信息,包括订购日期、发货日期和到货日期。假设这些信息分别存储在名为 `Orders`, `OrderDetails`, 和 `Shipments` 的表中,你可以使用以下 SQL 查询:
1. 创建一个辅助视图(如果不存在)来连接这些表并计算发货期和运货期:
```sql
CREATE VIEW OrdersWithTimeline AS
SELECT
o.OrderID,
od.OrderDate AS OrderDate,
so.ShippedDate AS ShippedDate,
si.ReceivedDate AS ReceivedDate,
DATEDIFF(day, od.OrderDate, so.ShippedDate) AS ShippingPeriod,
DATEDIFF(day, so.ShippedDate, si.ReceivedDate) AS DeliveryPeriod
FROM
Orders o
JOIN
OrderDetails od ON o.OrderID = od.OrderID
JOIN
Shipments so ON od.ProductID = so.ProductID
JOIN
Suppliers s ON so.SupplierID = s.SupplierID;
```
这将计算每笔订单的发货期(从订单日期到发货日期)和运货期(从发货日期到收货日期)。
2. 排序查询结果按照发货期从长到短:
```sql
SELECT * FROM OrdersWithTimeline
ORDER BY ShippingPeriod DESC;
```
阅读全文