我正在尝试确定为什么我从此查询中获得多个结果。我需要确保每个UserId只有1个结果。
declare @UserId INT = 1
SELECT u.UserId,
cfeLead.ElementName AS 'LeadSource' ,
rt.ReferralType AS 'MarketingSource' ,
cfe.ElementName AS 'Corporate'
FROM Users u
LEFT JOIN ReferralType rt ON rt.ReferralTypeID = u.ReferralTypeID
LEFT JOIN UserCustomFields ucf ON ucf.UserID = u.UserID
LEFT JOIN CustomFields cf ON cf.CustomFieldID = ucf.CustomFieldID
AND (cf.CustomFieldName = 'Corporate' OR cf.CustomFieldName = 'Corporate Membership')
LEFT JOIN CustomFieldElements cfe ON cfe.CustomFieldID = cf.CustomFieldID
AND ucf.CustomFieldElementID = cfe.CustomFieldElementID
LEFT JOIN CustomFields cfLead ON cfLead.CustomFieldID = ucf.CustomFieldID
AND cfLead.CustomFieldName = 'Lead Source'
LEFT JOIN CustomFieldElements cfeLead ON cfeLead.CustomFieldID = cfLead.CustomFieldID
AND ucf.CustomFieldElementID = cfeLead.CustomFieldElementID
WHERE u.UserID = @UserId
我的结果显示:
UserId LeadSource MarketingSource Corporate
1 NULL DirectMail 180 Center
1 Walkin DirectMail NULL
我想看看:
UserId LeadSource MarketingSource Corporate
1 Walkin DirectMail 180 Center
我的假设是多个左连接引起了这种情况,但我似乎无法追踪它。谢谢大家。
答案 0 :(得分:1)
使用Coalesce
declare @UserId INT = 1
SELECT distinct u.UserId,
Coalesce(cfeLead.ElementName, cfe.ElementName) AS 'LeadSource' ,
rt.ReferralType AS 'MarketingSource' ,
Coalesce(cfe.ElementName, cfeLead.ElementName) AS 'Corporate'
FROM Users u
LEFT JOIN ReferralType rt ON rt.ReferralTypeID = u.ReferralTypeID
LEFT JOIN UserCustomFields ucf ON ucf.UserID = u.UserID
LEFT JOIN CustomFields cf ON cf.CustomFieldID = ucf.CustomFieldID
AND (cf.CustomFieldName = 'Corporate' OR cf.CustomFieldName = 'Corporate Membership')
LEFT JOIN CustomFieldElements cfe ON cfe.CustomFieldID = cf.CustomFieldID
AND ucf.CustomFieldElementID = cfe.CustomFieldElementID
LEFT JOIN CustomFields cfLead ON cfLead.CustomFieldID = ucf.CustomFieldID
AND cfLead.CustomFieldName = 'Lead Source'
LEFT JOIN CustomFieldElements cfeLead ON cfeLead.CustomFieldID = cfLead.CustomFieldID
AND ucf.CustomFieldElementID = cfeLead.CustomFieldElementID
WHERE u.UserID = @UserId
使用Left Join并且您想要UN-NULL值时,在所有已连接的表上使用Coalesce
答案 1 :(得分:1)
我几乎可以肯定,JOIN谓词与条件谓词的混合是问题的根源。如果我正确,那么以下代码应该正常工作并且只返回一行:
DECLARE @UserId INT = 1
SELECT u.UserId
, cfeLead.ElementName AS 'LeadSource'
, rt.ReferralType AS 'MarketingSource'
, cfe.ElementName AS 'Corporate'
FROM Users u
LEFT JOIN ReferralType rt ON rt.ReferralTypeID = u.ReferralTypeID
LEFT JOIN UserCustomFields ucf ON ucf.UserID = u.UserID
LEFT JOIN CustomFields cf ON cf.CustomFieldID = ucf.CustomFieldID
LEFT JOIN CustomFieldElements cfe
ON cfe.CustomFieldID = cf.CustomFieldID
AND ucf.CustomFieldElementID = cfe.CustomFieldElementID
LEFT JOIN CustomFields cfLead ON cfLead.CustomFieldID = ucf.CustomFieldID
LEFT JOIN CustomFieldElements cfeLead
ON cfeLead.CustomFieldID = cfLead.CustomFieldID
AND ucf.CustomFieldElementID = cfeLead.CustomFieldElementID
WHERE u.UserID = @UserId
AND (cf.CustomFieldName = 'Corporate'
OR cf.CustomFieldName = 'Corporate Membership')
AND cfLead.CustomFieldName = 'Lead Source'
答案 2 :(得分:0)
好的,所以花了比我想要的更长的时间,但我想发布我的答案。
declare @UserId INT = 1
SELECT u.UserId,
MAX(cfeLead.ElementName) AS 'LeadSource' ,
MAX(rt.ReferralType) AS 'MarketingSource' ,
MAX(cfe.ElementName) AS 'Corporate'
FROM Users u
LEFT JOIN ReferralType rt ON rt.ReferralTypeID = u.ReferralTypeID
LEFT JOIN UserCustomFields ucf ON ucf.UserID = u.UserID
LEFT JOIN CustomFields cf ON cf.CustomFieldID = ucf.CustomFieldID
AND (cf.CustomFieldName = 'Corporate' OR cf.CustomFieldName = 'Corporate Membership')
LEFT JOIN CustomFieldElements cfe ON cfe.CustomFieldID = cf.CustomFieldID
AND ucf.CustomFieldElementID = cfe.CustomFieldElementID
LEFT JOIN CustomFields cfLead ON cfLead.CustomFieldID = ucf.CustomFieldID
AND cfLead.CustomFieldName = 'Lead Source'
LEFT JOIN CustomFieldElements cfeLead ON cfeLead.CustomFieldID = cfLead.CustomFieldID
AND ucf.CustomFieldElementID = cfeLead.CustomFieldElementID
WHERE u.UserID = @UserId
GROUP BY u.UserId
这样做就像一个魅力。