1

我正在运行这个查询:

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

4

2 回答 2

2

cross applyouter apply. 像这样的东西:

SELECT DISTINCT A.PeripheralNumber, P.LoginName, P.FirstName, P.LastName, A.EnterpriseName, A.AgentDeskSettingsID, DS.AttributeValues
FROM Agent A
LEFT JOIN Person P
    ON A.PersonID = P.PersonID
LEFT JOIN Agent_Desk_Settings ADS
    ON A.AgentDeskSettingsID = ADS.AgentDeskSettingsID
OUTER APPLY
(
    SELECT STUFF((SELECT ',' + ATT.EnterpriseName+'='+AT.AttributeValue AS Attribute
    FROM Agent_Attribute AT
    INNER JOIN Attribute ATT
        ON ATT.AttributeID = AT.AttributeID
    WHERE A.SkillTargetID = AT.SkillTargetID
    ORDER BY Attribute
    FOR XML PATH (''), TYPE ).value('.', 'VARCHAR(MAX)'), 1, 1, '')
) DS (AttributeValues)
WHERE A.SkillTargetID IN ('26379','24111')
    AND A.EnterpriseName LIKE 'PG_8%'
    AND P.FirstName NOT LIKE '%Test%'
于 2020-08-12T14:04:46.340 回答
2

如果要将值限制为单个人,则需要关联该人:

STUFF((SELECT ',' + ATT.EnterpriseName+'='+AT.AttributeValue AS Attribute
       FROM Agent_Attribute AT JOIN
            Agent A 
            ON A.SkillTargetID = AT.SkillTargetID
            Attribute ATT
            ON ATT.AttributeID = AT.AttributeID
            WHERE A.SkillTargetID IN (26379, 24111) AND
                  AA.PersonID = p.PersonId
            ORDER BY Attribute
            FOR XML PATH (''), TYPE
           ).value('.', 'VARCHAR(MAX)'), 1, 1, '')

请注意,我从看起来像数字常量的内容中删除了单引号。如果这些确实是字符串,则包括单引号。

此外,LEFT JOINs 在相关子查询中是多余的。

于 2020-08-12T14:05:53.373 回答