[英]Comparing two dates in SQL in where clause using CASE WHEN?
I am using following query which is working fine for me.我正在使用以下查询,这对我来说很好用。
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))
Now I have to make date compare cause conditional in where clause using case when something like this;现在我必须在 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
Can something please guide how can I achieve this.可以请指导我如何实现这一目标。 I have tried but still unable to get it done.我已经尝试过,但仍然无法完成。
Try something using AND OR使用 AND OR 尝试一些东西
possibly可能
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.