如何使用 STUFF 和 FOR XML PATH 分隔与其行对应的唯一值
How to separate unique values respective to its row using STUFF and FOR XML PATH
我运行这个查询:
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 的唯一属性值正在合并。我需要的结果如下:
|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 服务器 2016
你cross apply
或outer 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%'
如果要将值限制为单个人,则需要与人相关联:
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 JOIN
在相关子查询中是多余的。
我运行这个查询:
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 的唯一属性值正在合并。我需要的结果如下:
|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 服务器 2016
你cross apply
或outer 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%'
如果要将值限制为单个人,则需要与人相关联:
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 JOIN
在相关子查询中是多余的。