我的查询如下:
SELECT Delivery.DeliveryNumber,
DeliveryStatus.StatusName,
Delivery.PickupDateTime,
Delivery.DeliveryDateTime,
Delivery.PackageWeight,
Delivery.PackageSize,
Delivery.PickupAddress1,
CASE
WHEN Delivery.DeliveryDateTime IS NULL THEN 'Not Delivered'
WHEN (DATEDIFF(hh, Delivery.PickupDateTime, Delivery.DeliveryDateTime) > 0)
AND (DATEDIFF(hh, Delivery.PickupDateTime, Delivery.DeliveryDateTime) < 24) THEN 'Delivered 24hrs After Pickup'
WHEN (DATEDIFF(dd, Delivery.PickupDateTime, Delivery.DeliveryDateTime) = 0) THEN 'Delivered Same Day'
END AS Status
FROM Delivery
INNER JOIN DeliveryStatus ON Delivery.StatusCode = DeliveryStatus.StatusCode
此查询的问题是状态现在可以有NULL
个值。我不希望Status
拥有NULLs
。
所以我尝试添加:
WHERE (Status IS NOT NULL)
但是我知道无论如何我也做不到这个......我不想这样做:
WHERE CASE
WHEN Delivery.DeliveryDateTime IS NULL THEN 'Not Delivered'
WHEN (DATEDIFF(hh, Delivery.PickupDateTime, Delivery.DeliveryDateTime) > 0)
AND (DATEDIFF(hh, Delivery.PickupDateTime, Delivery.DeliveryDateTime) < 24) THEN 'Delivered 24hrs After Pickup'
WHEN (DATEDIFF(dd, Delivery.PickupDateTime, Delivery.DeliveryDateTime) = 0) THEN 'Delivered Same Day'
有没有办法清理查询?
我的任务是设计一个查询,使其返回满足以下条件的行:
我认为我的查询正确。我只是需要帮助清理它,因为它看起来很脏。有什么想法吗?
编辑:添加了整个数据结构:
答案 0 :(得分:1)
所以你的问题是你不想两次编码相同的逻辑(一次在where子句中,一次在选择列表中)。您可以使用外部查询从您已有的行中选择所需的行来解决此问题:
SELECT *
FROM
(
SELECT Delivery.DeliveryNumber,
DeliveryStatus.StatusName,
Delivery.PickupDateTime,
Delivery.DeliveryDateTime,
Delivery.PackageWeight,
Delivery.PackageSize,
Delivery.PickupAddress1,
CASE
WHEN Delivery.DeliveryDateTime IS NULL THEN 'Not Delivered'
WHEN (DATEDIFF(hh, Delivery.PickupDateTime, Delivery.DeliveryDateTime) > 0)
AND (DATEDIFF(hh, Delivery.PickupDateTime, Delivery.DeliveryDateTime) < 24) THEN 'Delivered 24hrs After Pickup'
WHEN (DATEDIFF(dd, Delivery.PickupDateTime, Delivery.DeliveryDateTime) = 0) THEN 'Delivered Same Day'
END AS Status
FROM Delivery
INNER JOIN DeliveryStatus ON Delivery.StatusCode = DeliveryStatus.StatusCode
)
WHERE Status IS NOT NULL;