Сравнивая две даты в SQL в предложении where с помощью CASE WHEN? - PullRequest
0 голосов
/ 20 февраля 2020

Я использую следующий запрос, который работает нормально для меня.

   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

Может ли что-нибудь помочь, как мне этого добиться. Я пытался, но все еще не смог сделать это.

1 Ответ

0 голосов
/ 20 февраля 2020

Попробуйте что-нибудь, используя ИЛИ ИЛИ

возможно

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
        )
...