我正在尝试将以下行合并到一个记录中。我希望将[贷款人员]和[房地产经纪人]的名字显示在一行上。任何帮助将不胜感激。
这是我现在的位置
SELECT DISTINCT z.State
,z.County
,z.City
,cr.ZipCode
,cr.Route
,UPPER(c1.LastName) + ', ' + UPPER(c1.FirstName) AS 'Loan Officer'
,UPPER(c2.LastName) + ', ' + UPPER(c2.FirstName) AS 'Realtor'
,cr.ID
FROM Prospecting.ZipCodeCarrierRoute cr WITH (NOLOCK)
INNER JOIN ZIPCodes z WITH (NOLOCK)
ON z.ZipCode = cr.ZipCode
LEFT JOIN Prospecting.Contact_CarrierCode_Assignments cca WITH (NOLOCK)
ON cca.ZipCodeCarrierRouteID = cr.ID
LEFT JOIN Prospecting.Contact c1 WITH (NOLOCK)
ON c1.ContactID = cca.ContactID
AND c1.ContactTypeID = 1
LEFT JOIN Prospecting.Contact c2 WITH (NOLOCK)
ON c2.ContactID = cca.ContactID
AND c2.ContactTypeID = 2
WHERE cr.ID = 875
这是结果
这就是我要找的东西
答案 0 :(得分:3)
您可以使用group by
:
SELECT z.State, z.County, z.City, cr.ZipCode, cr.Route,
MAX(UPPER(c1.LastName) + ', ' + UPPER(c1.FirstName)) AS [Loan Officer],
MAX(UPPER(c2.LastName) + ', ' + UPPER(c2.FirstName)) AS [Realtor],
cr.ID
FROM Prospecting.ZipCodeCarrierRoute cr WITH (NOLOCK)
INNER JOIN ZIPCodes z WITH (NOLOCK)
ON z.ZipCode = cr.ZipCode
LEFT JOIN Prospecting.Contact_CarrierCode_Assignments cca WITH (NOLOCK)
ON cca.ZipCodeCarrierRouteID = cr.ID
LEFT JOIN Prospecting.Contact c1 WITH (NOLOCK)
ON c1.ContactID = cca.ContactID
AND c1.ContactTypeID = 1
LEFT JOIN Prospecting.Contact c2 WITH (NOLOCK)
ON c2.ContactID = cca.ContactID
AND c2.ContactTypeID = 2
WHERE cr.ID = 875
GROUP z.State, z.County, z.City, cr.ZipCode, cr.Route, cr.ID