如何GROUP BY CASE语句返回的子查询值

时间:2013-07-04 10:52:25

标签: sql-server-2008 tsql group-by subquery

我一直在争取找到答案。它是我维护的代码,没有错误。它位于存储过程中,用于返回报告的数据。

为了便于阅读,我把它缩小了,只包含了必要的部分。

它没有错误,但已经发出请求,要求它将相应行中的所有相关数据分组。

主要问题是对包含SUBQUERY的第三个CASE语句应用GROUP BY语句:

    SELECT
            CompanyName,
            CompanyRef,
            Amount,
            Reference,
            VendorNo,
            CASE ErrorID WHEN 10 'Company Name: ' + CompanyName + 'CompanyRef' END AS InvalidCompanyNameForRef,
            CASE ErrorID WHEN 11 'Vendor Number:' + VendorNo END AS InvalidVendorNo,
            CASE ErrorID WHEN 12 (SELECT TOP(1) 'Non VAT Amount:' + CAST(PendingPayments.NonVatAmount AS varchar) + 'Import Amount:' + CAST(Imported.PaymentAmount) FROM PendingPayments
                        WHERE  Imported.CompanyRef = PendingPayments.CompanyRef
                           AND Imported.VendorNo = PendingPayments.VendorNo
                               AND ISNUMERIC(Imported.PaymentAmount) = 1
                           AND CAST(Imported.PaymentAmount AS MONEY) <> PendingPayments.NonVatAmount AND PendingPayments.isVAT = 0      
                         ) END As PaymentAmountMismatch
        .
        .
        .
GROUP BY
            CompanyName,
            CompanyRef,
            Amount,
            Reference,
            VendorNo,
            CASE ErrorID WHEN 10 'Company Name: ' + CompanyName + 'CompanyRef' END,
            CASE ErrorID WHEN 11 'Vendor Number:' + VendorNo END

如何根据从SUBQUERY派生的字段对其进行分组。

我希望它返回类似这样的内容,而不是为同一数据的不同列返回不同的行:

CompanyName | CompanyRef | Amount | Reference | VendorNo | InvalidCompanyNameForRef     | Invalid Vendor Number | PaymentAmountMismatch
------------+------------+--------+-----------+----------+------------------------------+-----------------------+-----------------------------------------+
ABC         | 1          | 25.00  | INV001    |390       |Company Name: ABC CompanyRef:1|                       |   
DEF         | 5          | 12.00  | INV002    |212       |                              |                   212 |Expected Amount:12.50, Import Amount:12.00
HIJ         | 7          | 9.50   | INV003    |31        |Company Name: HIJ CompanyRef:7|                       |

1 个答案:

答案 0 :(得分:1)

在我看来,CTE can help you。代码没有经过测试,但我认为这足以让我们有所了解。

    WITH cte 
           (CompanyName,
            CompanyRef,
            Amount,
            Reference,
            VendorNo,
            InvalidCompanyNameForRef,
            InvalidVendorNo,
            PaymentAmountMismatch) AS
(
SELECT
CompanyName,
CompanyRef,
Amount,
Reference,
VendorNo,
CASE ErrorID WHEN 10 'Company Name: ' + CompanyName + 'CompanyRef' END AS InvalidCompanyNameForRef,
CASE ErrorID WHEN 11 'Vendor Number:' + VendorNo END AS InvalidVendorNo,
CASE ErrorID WHEN 12 (SELECT TOP(1) 'Non VAT Amount:' + CAST(PendingPayments.NonVatAmount AS varchar) + 'Import Amount:' + CAST(Imported.PaymentAmount) FROM PendingPayments
            WHERE  Imported.CompanyRef = PendingPayments.CompanyRef
                AND Imported.VendorNo = PendingPayments.VendorNo
                    AND ISNUMERIC(Imported.PaymentAmount) = 1
                AND CAST(Imported.PaymentAmount AS MONEY) <> PendingPayments.NonVatAmount AND PendingPayments.isVAT = 0      
                ) END As PaymentAmountMismatch
)
SELECT * FROM cte 
GROUP BY
    CompanyName,
    CompanyRef,
    Amount,
    Reference,
    VendorNo,
    InvalidCompanyNameForRef,
    InvalidVendorNo,
    PaymentAmountMismatch