Я использую следующий запрос, который работает нормально для меня.
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, когда что-то подобное;
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
Может ли что-нибудь помочь, как мне этого добиться. Я пытался, но все еще не смог сделать это.