还有其他写方法吗?如果“应用程序”表中的“信用卡ID”为NULL,则从“终止的信用卡”表中加入“信用卡ID”。谢谢,
这是错误:
消息4104,第16级,状态1,第18行
不能绑定多部分标识符“ TC.CreditCardID”。
代码:
SELECT DISTINCT
prg.Title AS Program, a.Patientid,
a. Applicationid,
PT.MCC,
PT.MerchantName,
PT.MerchantCity, PT.MerchantState,
PT.MerchantZip,
PT.SettlementTransactionID,
CONVERT(DATE, PT.SettlementDate) AS SettlementDate
ABS(PT.Amount) as TransactionAmount
FROM
[dbo].[StagingSettlements] PT
LEFT JOIN
dbo.Application a ON PT.[CustomId] = ISNULL(a.CreditCardId, TC.CreditCardID)
LEFT JOIN
dbo.TerminatedCreditCard TC ON TC.ApplicationId = a.ApplicationId
答案 0 :(得分:0)
几点:
下面的SQL应该可以解决您的问题,我不确定StagingSettlements和应用程序之间的连接条件是什么,所以我刚刚放入了一个占位符,需要对其进行更新,我还添加了一个'CreditCardId '进行选择:
SELECT Distinct
prg.Title as Program
,a.Patientid
,a.Applicationid
,PT.MCC
,PT.MerchantName
,PT.MerchantCity
,PT.MerchantState
,PT.MerchantZip
,PT.SettlementTransactionID
,convert(Date,PT.SettlementDate) as SettlementDate
,ABS(PT.Amount) as TransactionAmount
,COALESCE(TC.CreditCardId, a.CreditCardId) as CreditCardId
FROM [dbo].[StagingSettlements] PT
LEFT JOIN dbo.Application a
ON a.<applicationid> = PT.<applicationid>
LEFT JOIN dbo.TerminatedCreditCard TC
ON TC.ApplicationId = a.ApplicationId
AND a.CreditCardId IS NULL
答案 1 :(得分:0)
您可以更改联接precedence来获取此信息:
FROM [dbo].[StagingSettlements] AS PT
LEFT JOIN ( dbo.Application AS a
LEFT JOIN dbo.TerminatedCreditCard AS TC on TC.ApplicationId = a.ApplicationId )
ON PT.[CustomId] = ISNULL( a.CreditCardId, TC.CreditCardID )
上面加上括号会导致首先对Application
至TerminatedCreditCard
的连接进行求值,然后将结果连接至StagingSettlements
。