我正在运行这个查询:
SELECT A.PeripheralNumber, P.LoginName, P.FirstName, P.LastName, A.EnterpriseName, A.AgentDeskSettingsID, AttributeValues =
STUFF((SELECT ',' + ATT.EnterpriseName+'='+AT.AttributeValue AS Attribute
FROM Agent_Attribute AT
LEFT JOIN Agent A ON A.SkillTargetID = AT.SkillTargetID
LEFT JOIN Attribute ATT ON ATT.AttributeID = AT.AttributeID
WHERE A.SkillTargetID IN ('26379','24111')
ORDER BY Attribute
FOR XML PATH (''), TYPE ).value('.', 'VARCHAR(MAX)'), 1, 1, '')
FROM Agent A
LEFT JOIN Person P ON P.PersonID = A.PersonID
LEFT JOIN Agent_Desk_Settings ADS ON ADS.AgentDeskSettingsID = A.AgentDeskSettingsID
WHERE A.SkillTargetID IN ('26379','24111')
AND A.EnterpriseName LIKE 'PG_8%'
AND P.FirstName NOT LIKE '%Test%'
GROUP BY A.PeripheralNumber, P.LoginName, P.FirstName, P.LastName, A.EnterpriseName, A.AgentDeskSettingsID
我得到的结果如下:
|PeripheralNumber|LoginName|FirstName|LastName|EnterpriseName|AgentDeskSettingsID|AttributeValues |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|551628 |551628 |John |Doe |Doe_John |5104 |AAH_CSC_EXIST=5,AAH_CSC_FNOL=5,AAH_CSC_ROTL=5,AAH_SERVICE_AUTO=10,AAH_SERVICE_REBRAND=5,AAH_SERVICE_WEB_ONLINE=10|
|558792 |558792 |Jane |Doe |Doe_Jane |5103 |AAH_CSC_EXIST=5,AAH_CSC_FNOL=5,AAH_CSC_ROTL=5,AAH_SERVICE_AUTO=10,AAH_SERVICE_REBRAND=5,AAH_SERVICE_WEB_ONLINE=10|
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
正在发生的事情是正在合并 John Doe 和 Jane Doe 的唯一 AttributeValues。我需要的结果如下:
|PeripheralNumber|LoginName|FirstName|LastName|EnterpriseName|AgentDeskSettingsID|AttributeValues |
------------------------------------------------------------------------------------------------------------------------------------------------------
|551628 |551628 |John |Doe |Doe_John |5104 |AAH_SERVICE_AUTO=10,AAH_SERVICE_REBRAND=5,AAH_SERVICE_WEB_ONLINE=10|
|558792 |558792 |Jane |Doe |Doe_Jane |5103 |AAH_CSC_EXIST=5,AAH_CSC_FNOL=5,AAH_CSC_ROTL=5 |
------------------------------------------------------------------------------------------------------------------------------------------------------
帮助!这是 MS SQL Server 2016