0
我一直在努力尋找答案。這是我維護的代碼,沒有錯誤。它位於爲報表返回數據的存儲過程中。如何GROUP BY由CASE語句返回的子查詢值
爲了便於閱讀,我將它縮小了,只是包含了必要的部分。
它的工作原理沒有錯誤,但已經提出了一個請求,使其將相應行中的所有相關數據分組。
主要問題是應用GROUP BY語句包含一個子查詢的第三個CASE語句:
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
如何使IT部門從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| |
Thanks @valiik!你指出我在正確的方向 – user919426
不客氣。 – valiik