根据多个因素过滤SQL

时间:2015-03-04 10:24:00

标签: sql sql-server

我在SQL Server 2008中有一个查询,它从Sage中提取订单数据。此查询仅提取当前订单并显示它们是否正在收集并已分派。这是查询

SELECT SOPOrderReturn.DocumentNo,
                   CONVERT(date, SOPOrderReturn.DocumentDate) AS DocumentDate,
                   SLCustomerAccount.CustomerAccountName,
                   CONVERT(Date, SOPOrderReturn.RequestedDeliveryDate) AS RequestedDeliveryDate,
                   SOPDocDelAddress.PostalName,
                   POPOrderReturn.SupplierDocumentNo,
                   PLSupplierAccount.SupplierAccountName,
                   SOPDespatchReceiptType.Name
            FROM SOPOrderReturn
            INNER JOIN SLCustomerAccount ON SOPOrderReturn.CustomerID = SLCustomerAccount.SLCustomerAccountID
            INNER JOIN SOPDocDelAddress ON SOPOrderReturn.SOPOrderReturnID = SOPDocDelAddress.SOPOrderReturnID
            LEFT OUTER JOIN SOPDespatchReceiptType
            INNER JOIN SOPDespatchReceipt ON SOPDespatchReceiptType.SOPDespatchReceiptTypeID = SOPDespatchReceipt.SOPDespatchReceiptTypeID ON SOPOrderReturn.SOPOrderReturnID = SOPDespatchReceipt.SOPOrderID
            LEFT OUTER JOIN PLSupplierAccount
            INNER JOIN POPOrderReturn ON PLSupplierAccount.PLSupplierAccountID = POPOrderReturn.SupplierID ON RIGHT(SOPOrderReturn.DocumentNo, 6) = POPOrderReturn.SupplierDocumentNo
            GROUP BY SOPOrderReturn.DocumentDate,
                     SLCustomerAccount.CustomerAccountName,
                     SOPOrderReturn.DocumentNo,
                     SLCustomerAccount.AccountIsOnHold,
                     SOPOrderReturn.RequestedDeliveryDate,
                     SOPDocDelAddress.PostalName,
                     POPOrderReturn.SupplierDocumentNo,
                     PLSupplierAccount.SupplierAccountName,
                     SOPDespatchReceiptType.Name
            HAVING (SLCustomerAccount.CustomerAccountName <> 'Cash Sales')
            AND (SLCustomerAccount.CustomerAccountName <> 'Staff Purchases')
            AND (SLCustomerAccount.CustomerAccountName <> 'Samples')
            ORDER BY RequestedDeliveryDate

此查询的结果如下所示:

DocumentNo  DocumentDate    CustomerAccountName RequestedDeliveryDate   PostalName  SupplierDocumentNo  SupplierAccountName Name
233510      03/03/2015      CustomerName            03/03/2015         PostalName           NULL            NULL            NULL
233497      02/03/2015      CustomerName            03/03/2015          Collection          NULL            NULL            Despatch Note
233507      03/03/2015      CustomerName            10/03/2015          PostalName          NULL            NULL            Despatch Note 
233503      03/03/2015      CustomerName            03/03/2015          PostalName          NULL            NULL            NULL
233504      03/03/2015      CustomerName            03/03/2015          PostalName          NULL            NULL            NULL
233509      03/03/2015      CustomerName            03/03/2015          PostalName          NULL            NULL            NULL
233478      02/03/2015      CustomerName            04/03/2015          Collection          NULL            NULL            NULL
233501      02/03/2015      CustomerName            04/03/2015          PostalName          233501          name            Despatch Note
233462      27/02/2015      CustomerName            04/03/2015          PostalName          233462          name            Despatch Note
233499      02/03/2015      CustomerName            05/03/2015          Collection          NULL            NULL            NULL
233431      25/02/2015      CustomerName            06/03/2015          Collection          NULL            NULL            NULL
233434      25/02/2015      CustomerName            06/03/2015          PostalName          NULL            NULL            NULL
233506      03/03/2015      CustomerName            09/03/2015          PostalName          NULL            NULL            NULL
233513      04/03/2015      CustomerName            10/03/2015          PostalName          NULL            NULL            Despatch Note
233507      03/03/2015      CustomerName            10/03/2015          PostalName          NULL            NULL            Despatch Note
233446      26/02/2015      CustomerName            10/03/2015          PostalName          NULL            NULL            Despatch Note   
233488      02/03/2015      CustomerName            16/03/2015          PostalName          NULL            NULL            NULL
232975      30/01/2015      CustomerName            23/03/2015          PostalName          NULL            NULL            NULL

不同的装运方式

Collection = Anything with a PostalName = Collection regardless of any other fields

Hauiler = Anything with SupplierDocumentNo  that IS NOT null 

Delivery by us = Any PostalName that is not Collection and SupplierDocumentNo   is null.

那么我需要过滤掉我们发货并已交付的结果订单,我们可以通过在名称字段中它是否等于发货单来说明

所以结果集的第三行是由我们发送的,因为PostalName不是Collection和SupplierDocumentNo IS NUll,而Name = Despatch Note

但是第一行也是由我们发货但名称!=发货注意所以它尚未发货,所以这仍然需要在结果集中

以下是结果集应该是什么样的

    DocumentNo  DocumentDate    CustomerAccountName RequestedDeliveryDate   PostalName  SupplierDocumentNo  SupplierAccountName Name
233510      03/03/2015      CustomerName            03/03/2015         PostalName           NULL            NULL            NULL
233497      02/03/2015      CustomerName            03/03/2015          Collection          NULL            NULL            Despatch Note
Row filtered out
233503      03/03/2015      CustomerName            03/03/2015          PostalName          NULL            NULL            NULL
233504      03/03/2015      CustomerName            03/03/2015          PostalName          NULL            NULL            NULL
233509      03/03/2015      CustomerName            03/03/2015          PostalName          NULL            NULL            NULL
233478      02/03/2015      CustomerName            04/03/2015          Collection          NULL            NULL            NULL
233501      02/03/2015      CustomerName            04/03/2015          PostalName          233501          name            Despatch Note
233462      27/02/2015      CustomerName            04/03/2015          PostalName          233462          name            Despatch Note
233499      02/03/2015      CustomerName            05/03/2015          Collection          NULL            NULL            NULL
233431      25/02/2015      CustomerName            06/03/2015          Collection          NULL            NULL            NULL
233434      25/02/2015      CustomerName            06/03/2015          PostalName          NULL            NULL            NULL
233506      03/03/2015      CustomerName            09/03/2015          PostalName          NULL            NULL            NULL
Row filtered out
Row filtered out
Row filtered out
233488      02/03/2015      CustomerName            16/03/2015          PostalName          NULL            NULL            NULL
232975      30/01/2015      CustomerName            23/03/2015          PostalName          NULL            NULL            NULL

1 个答案:

答案 0 :(得分:1)

添加以下WHERE CLAUSE:

WHERE PostalName = 'Collection' OR SupplierDocumentNo IS NOT NULL OR Name != 'Despatch Note'

GROUP BY之前。