G-day All,
我有一个运行相对较好的SQL语句,但是在尝试包含其他条件时遇到问题,我相信这是由于我的操作顺序,但我似乎无法弄明白。请参阅下面的当前代码:
SELECT
WorkOrderNumber,
AssetDescription,
AssetNumber,
CASE
WHEN WorkOrderDescription IS NULL
THEN WorkOrderHistoryDescription
ELSE WorkOrderDescription
END AS RevWorkOrderDescription,
RaisedDateTime,
FinishedDateTime,
CASE
WHEN PriorityDescription = 'Critical > 4 hours'
THEN 'P1'
WHEN PriorityDescription = 'High > 8 hours'
THEN 'P2'
WHEN PriorityDescription = 'Medium > 48 hours'
THEN 'P3'
WHEN PriorityDescription = 'Low > 72 hours'
THEN 'P4'
ELSE 'Shutdown - no end date'
END AS Priority,
ComponentCodeName,
(dbo.Contact.FirstName + ' ' + dbo.Contact.LastName) AS RaisedBy
FROM dbo.WorkOrder
LEFT JOIN dbo.Asset ON dbo.Asset.AssetID = dbo.WorkOrder.AssetID
LEFT JOIN dbo.ComponentCode ON dbo.ComponentCode.ComponentCodeID = dbo.WorkOrder.ComponentCodeID
LEFT JOIN dbo.Priority ON dbo.Priority.PriorityID = dbo.WorkOrder.PriorityID
LEFT JOIN dbo.Contact ON dbo.Contact.ContactID = dbo.WorkOrder.CreatedByContactID
WHERE (
(
RaisedDateTime BETWEEN '2015-05-01 00:00:00.000'
AND '2015-05-31 23:59:59.999'
)
OR (
FinishedDateTime BETWEEN '2015-05-01 00:00:00.000'
AND '2015-05-31 23:59:59.999'
AND RaisedDateTime < '2015-05-01 00:00:00.000'
)
)
AND PreventativeMaintenanceID IS NULL
ORDER BY WorkOrder.WorkOrderNumber
我要做的是包含所有 PreventativeMaintenanceID 为NULL的WorkOrder,或者返回所有 dbo.PreventativeMaintenance.IsStandard 值为1的WorkOrders。当我尝试添加联接并包含AND dbo.WorkOrder.PreventativeMaintenanceID IS NULL OR dbo.PreventativeMaintenance.IsStandard
时,查询似乎忽略了我选择的日期范围。
非常感谢任何帮助,在SQL方面我仍然相对绿色,因此对代码平衡的任何评论都会很棒。