我有2张桌子
表1 - PaymentMethod
具有以下信息
ComCd | AccountGroup | BankCountry | Currency | VARSPaymentMethod | SAPPaymentMethod
_____________________________________________________________________________________
SG00 ABC SGD Check C
SG00 ABC SGD Return Check E
SG00 ABC Not SGD Check D
SG00 XYZ SGD Check C
SG00 XYZ SGD Return Check E
SG00 XYZ Not SGD Check D
表2 - [Vendor Tool Input]
包含以下信息
VendorNo | AccountGroup | BankCountry | ComCode | PaymentMethod | Payment Currency
__________________________________________________________________________________
TEST1 ABC SG00 Check SGD
TEST2 ABC SG00 Return Check SGD
TEST4 ABC SG00 Check AHP
TEST6 XYZ SG00 Check SGD
TEST7 XYZ SG00 Return Check SGD
TEST9 XYZ SG00 Check AHP
预期输出 -
VendorNo(from table 2) | sappaymentmethod(from table 1)
_______________________________________________________________
TEST1 C
TEST2 E
TEST4 D
TEST6 C
TEST7 E
TEST9 D
这是我写过的查询,但它看起来不起作用 -
SELECT A.[SAPPaymentMethod] ,B.[VendorNo],B.[ComCode],B.PaymentMethod,A.Currency,B.[Payment Currency] FROM [master].[dbo].[PaymentMethod] A, [master].[dbo].[Vendor Tool Input] B Where B.ComCode
=A.Comcd and B.AccountGroup=A.AccountGroup and B.BankCountry is null and B.[Payment Currency]=A.Currency and B.PaymentMethod=A.VARSPaymentMethod
答案 0 :(得分:0)
;WITH CTE1 AS
(
SELECT ComCd,AccountGroup ,SAPPaymentMethod,ROW_NUMBER() OVER(PARTITION BY AccountGroup ORDER BY SAPPaymentMethod) RNOPAY
FROM PaymentMethod
)
,
CTE2 AS
(
SELECT ComCd,VendorNo , AccountGroup,
ROW_NUMBER() OVER(PARTITION BY AccountGroup ORDER BY VendorNo) RNOVEN
FROM VendorToolInput
)
SELECT CTE2.VendorNo,CTE1.SAPPaymentMethod
FROM CTE1
JOIN CTE2 ON CTE1.AccountGroup=CTE2.AccountGroup AND CTE1.RNOPAY=CTE2.RNOVEN
AND CTE1.ComCd=CTE2.ComCd