我想列出所有鏈接到一個特定的「互動」我希望員工與逗號分隔它們一個行中列出的員工,我有工作,但下面的查詢很慢。逗號串聯結果分隔列表
SELECT
CONVERT(VARCHAR(254),
COALESCE(
STUFF(
(
SELECT
', ' + LTRIM(RTRIM(REPLACE(emp1.Full_Name,CHAR(9),'')))
FROM Employee emp1 WITH (nolock)
JOIN Rn_Int_Employee rie WITH (nolock) on emp1.Employee_Id = rie.Employee_Id
WHERE rnint.Rn_Interactions_Id = rie.Rn_Interactions_Id
GROUP BY emp1.Full_Name
ORDER BY emp1.Full_Name
FOR XML PATH (''),TYPE).value('text()[1]', 'nvarchar(max)'), 1, 2, '')
,STUFF((
SELECT
', ' + LTRIM(RTRIM(REPLACE(emp1.Full_Name,CHAR(9),'')))
FROM Employee emp1 WITH (nolock)
WHERE rnint.Rn_Create_User=emp1.Rn_Employee_User_Id
GROUP BY emp1.Full_Name
ORDER BY emp1.Full_Name
FOR XML PATH (''),TYPE).value('text()[1]', 'nvarchar(max)'), 1, 2, '')))
AS Employees
FROM Rn_Interactions rnint
有沒有人有這樣做的更有效的方式提出任何建議?
你可能想嘗試codereview.stackexchange.com – JeffUK
發佈你的表DDL(包括索引),並添加特定版本的SQL Server的標籤。 –
高效?這是一個耗時的查詢嗎?瓶頸在哪裏(看執行計劃)? –