使用 DISTINCT 时在 SQL 中重复

Duplicates in SQL when using DISTINCT

不确定为什么我总是收到此查询的重复项。这应该很容易,但由于某种原因我就是想不通。

这是我的查询:

SELECT DISTINCT
    STUFF((SELECT 
               ';  ' + 
               CASE
                   WHEN staff.lastname IS NOT NULL 
                       THEN UPPER(REPLACE(RTRIM(staff.lastname), ' ', '') + ', ' + RTRIM(staff.firstname))
                       ELSE UPPER('Not Assigned')
               END
           FROM 
               ca_case_assign ca 
           JOIN 
               staff ON staff.username = ca.staffusername
           JOIN 
               tbl_case c on ca.appid = c.col_caseid
           WHERE 
               ca.clientusername = c.col_username 
           FOR XML PATH('')), 1, 1, '') [CaseManager]

这是我得到的结果:

LOCALSTAFF, THERESA;  LOCALSTAFF, THERESA;  O'MALLEY, ELLEN;  STAFF, STATE;  STAFF, STATE;  STAFF, STATE;  STAFF, STATE;  STAFF, STATE;  STAFF, BC;  STAFF, BC;  STAFF, BC;  STAFF, BC;  STAFF, BC;  STAFF, BC;  STAFF, BC;

这显然是不正确的。

请帮忙,谢谢。

内部查询应该有不同的:

SELECT
STUFF((SELECT DISTINCT 
           ';  ' + 
           CASE
               WHEN staff.lastname IS NOT NULL 
                   THEN UPPER(REPLACE(RTRIM(staff.lastname), ' ', '') + ', ' + RTRIM(staff.firstname))
                   ELSE UPPER('Not Assigned')
           END
       FROM 
           ca_case_assign ca 
       JOIN 
           staff ON staff.username = ca.staffusername
       JOIN 
           tbl_case c on ca.appid = c.col_caseid
       WHERE 
           ca.clientusername = c.col_username 
       FOR XML PATH('')), 1, 1, '') [CaseManager]