使用 CASE WHEN 比较 where 子句中 SQL 中的两个日期?
Comparing two dates in SQL in where clause using CASE WHEN?
我正在使用以下查询,它对我来说工作正常。
SELECT SO.OrderNumber, TransactionsLookup.TransactionName, Branches.new_Name AS Branchname, i.new_PostedDate,
min(i.new_PostedDate) over(partition by SO.OrderNumber) FirstPostedDate,
FROM SalesOrders AS SO INNER JOIN
Temp_Invoices i on SO.SalesOrderId = i.SalesOrderId INNER JOIN
Branches ON SO.new_Branch = Branches.new_BranchId INNER JOIN
StatusReasonsLookup sl on i.StatusCode = sl.Id
WHERE
SO.new_Transaction = @AbstractTransactionID
AND SO.new_Branch <> @BranchID_Metro
AND ((month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate))
OR (month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) = year(new_PostedDate))
OR (month(new_CanceledDate) = month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate)) OR (i.new_CanceledDate IS NULL))
现在我必须在 where 子句中使用 case when 像这样使日期比较原因成为条件;
SELECT SO.OrderNumber, TransactionsLookup.TransactionName, Branches.new_Name AS Branchname, i.new_PostedDate,
min(i.new_PostedDate) over(partition by SO.OrderNumber) FirstPostedDate,
FROM SalesOrders AS SO INNER JOIN
Temp_Invoices i on SO.SalesOrderId = i.SalesOrderId INNER JOIN
Branches ON SO.new_Branch = Branches.new_BranchId INNER JOIN
StatusReasonsLookup sl on i.StatusCode = sl.Id
WHERE
( CASE When sl.StatusCodeName = 'Canceled' Then ((month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate))
OR (month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) = year(new_PostedDate))
OR (month(new_CanceledDate) = month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate))
) ELSE (i.new_CanceledDate IS NULL)) END )
AND SO.new_Transaction = @AbstractTransactionID
AND SO.new_Branch <> @BranchID_Metro
请指导我如何实现。我已经尝试过但仍然无法完成。
尝试使用 AND OR
可能
SELECT SO.OrderNumber
,TransactionsLookup.TransactionName
,Branches.new_Name AS Branchname
,i.new_PostedDate
,min(i.new_PostedDate) OVER (PARTITION BY SO.OrderNumber) FirstPostedDate
FROM SalesOrders AS SO
INNER JOIN Temp_Invoices i ON SO.SalesOrderId = i.SalesOrderId
INNER JOIN Branches ON SO.new_Branch = Branches.new_BranchId
INNER JOIN StatusReasonsLookup sl ON i.StatusCode = sl.Id
WHERE (
(sl.StatusCodeName = 'Canceled'
AND (
month(new_CanceledDate) <> month(new_PostedDate)
AND year(new_CanceledDate) <> year(new_PostedDate)
OR month(new_CanceledDate) <> month(new_PostedDate)
AND year(new_CanceledDate) = year(new_PostedDate)
OR month(new_CanceledDate) = month(new_PostedDate)
AND year(new_CanceledDate) <> year(new_PostedDate)
)
OR sl.StatusCodeName != 'Canceled'
AND i.new_CanceledDate IS NULL)
AND SO.new_Transaction = @AbstractTransactionID
AND SO.new_Branch <> @BranchID_Metro
)
我正在使用以下查询,它对我来说工作正常。
SELECT SO.OrderNumber, TransactionsLookup.TransactionName, Branches.new_Name AS Branchname, i.new_PostedDate,
min(i.new_PostedDate) over(partition by SO.OrderNumber) FirstPostedDate,
FROM SalesOrders AS SO INNER JOIN
Temp_Invoices i on SO.SalesOrderId = i.SalesOrderId INNER JOIN
Branches ON SO.new_Branch = Branches.new_BranchId INNER JOIN
StatusReasonsLookup sl on i.StatusCode = sl.Id
WHERE
SO.new_Transaction = @AbstractTransactionID
AND SO.new_Branch <> @BranchID_Metro
AND ((month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate))
OR (month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) = year(new_PostedDate))
OR (month(new_CanceledDate) = month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate)) OR (i.new_CanceledDate IS NULL))
现在我必须在 where 子句中使用 case when 像这样使日期比较原因成为条件;
SELECT SO.OrderNumber, TransactionsLookup.TransactionName, Branches.new_Name AS Branchname, i.new_PostedDate,
min(i.new_PostedDate) over(partition by SO.OrderNumber) FirstPostedDate,
FROM SalesOrders AS SO INNER JOIN
Temp_Invoices i on SO.SalesOrderId = i.SalesOrderId INNER JOIN
Branches ON SO.new_Branch = Branches.new_BranchId INNER JOIN
StatusReasonsLookup sl on i.StatusCode = sl.Id
WHERE
( CASE When sl.StatusCodeName = 'Canceled' Then ((month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate))
OR (month(new_CanceledDate) <> month(new_PostedDate) AND year(new_CanceledDate) = year(new_PostedDate))
OR (month(new_CanceledDate) = month(new_PostedDate) AND year(new_CanceledDate) <> year(new_PostedDate))
) ELSE (i.new_CanceledDate IS NULL)) END )
AND SO.new_Transaction = @AbstractTransactionID
AND SO.new_Branch <> @BranchID_Metro
请指导我如何实现。我已经尝试过但仍然无法完成。
尝试使用 AND OR
可能
SELECT SO.OrderNumber
,TransactionsLookup.TransactionName
,Branches.new_Name AS Branchname
,i.new_PostedDate
,min(i.new_PostedDate) OVER (PARTITION BY SO.OrderNumber) FirstPostedDate
FROM SalesOrders AS SO
INNER JOIN Temp_Invoices i ON SO.SalesOrderId = i.SalesOrderId
INNER JOIN Branches ON SO.new_Branch = Branches.new_BranchId
INNER JOIN StatusReasonsLookup sl ON i.StatusCode = sl.Id
WHERE (
(sl.StatusCodeName = 'Canceled'
AND (
month(new_CanceledDate) <> month(new_PostedDate)
AND year(new_CanceledDate) <> year(new_PostedDate)
OR month(new_CanceledDate) <> month(new_PostedDate)
AND year(new_CanceledDate) = year(new_PostedDate)
OR month(new_CanceledDate) = month(new_PostedDate)
AND year(new_CanceledDate) <> year(new_PostedDate)
)
OR sl.StatusCodeName != 'Canceled'
AND i.new_CanceledDate IS NULL)
AND SO.new_Transaction = @AbstractTransactionID
AND SO.new_Branch <> @BranchID_Metro
)