我使用以下查询来获取数据
SELECT NMA.MsgAddress,CC.PagerNum,CC.Email,CC.VoiceNum
FROM [Table1] GEP (NOLOCK)
INNER JOIN [Table2] CC (NOLOCK)
ON GEP.PersonID = CC.PersonID
LEFT OUTER JOIN [Table3] NME (NOLOCK)
ON GEP.PAN = NME.Cardnumber
AND NME.Status = 2 --ACTIVE (Enrolment Status)
INNER JOIN [Table4] NMA (NOLOCK)
ON NME.MsgAddressUID = NMA.MsgAddressUID
AND NMA.Status = 1 --ACTIVE (Address Status)
INNER JOIN [Table5] NMAT (NOLOCK)
ON NMA.MsgAddressTypeUID = NMAT.MsgAddressTypeUID
AND NMAT.MsgAddressType IN ('MobileNumber','EMAIL')
WHERE GEP.PAN IN (TEST)
我得到的输出如下
需要将明确的结果集管道分隔如下。
9856235687|test@Testing.com|698754321|Prakash@gmail.com|123121212
MsgAddress列是具有多个条目的列。
答案 0 :(得分:1)
可能是这样的
WITH SomeCTE (MsgAddress,PagerNum,Email,VoiceNum)
AS (
SELECT NMA.MsgAddress,CC.PagerNum,CC.Email,CC.VoiceNum
FROM [Table1] GEP (NOLOCK)
INNER JOIN [Table2] CC (NOLOCK)
ON GEP.PersonID = CC.PersonID
LEFT OUTER JOIN [Table3] NME (NOLOCK)
ON GEP.PAN = NME.Cardnumber
AND NME.Status = 2 --ACTIVE (Enrolment Status)
INNER JOIN [Table4] NMA (NOLOCK)
ON NME.MsgAddressUID = NMA.MsgAddressUID
AND NMA.Status = 1 --ACTIVE (Address Status)
INNER JOIN [Table5] NMAT (NOLOCK)
ON NMA.MsgAddressTypeUID = NMAT.MsgAddressTypeUID
AND NMAT.MsgAddressType IN ('MobileNumber','EMAIL')
WHERE GEP.PAN IN (TEST)
)
SELECT STUFF((SELECT '|' + MsgAddress
FROM SomeCTE
FOR XML PATH('')) ,1,1,'')
+ '|' + MAX(CC.PagerNum) + '|' + MAX(CC.Email) + '|' + MAX(CC.VoiceNum) As Result
FROM SomeCTE
GROUP BY PagerNum,Email,VoiceNum