如何避免重复名称?

时间:2018-06-18 14:12:47

标签: sql sql-server

如何避免重复姓名?我已经尝试了不同的东西,但他们都没有工作

SELECT 
    (SELECT DISTINCT(SUBSTRING(REPLACE(resource.name, '*Deleted*', '') 
     FROM 1  
     FOR POSITION(',' IN GROUP_CONCAT(REPLACE(resource.name, '*Deleted*', '' )))-1))) AS lastname,
    organisation.keywords AS keywords
FROM 
    obj_resource resource
LEFT JOIN  
    rel_raci resource_raci  ON resource.ID = resource_raci.PERSON_ID
LEFT JOIN 
    obj_resource organisation on  organisation.ID =  resource_raci.OBJ_ID
 -- Gibt nur die markierten Massnahmen aus
WHERE 
    CONTAINS('463bb931:158af3df8f6:1d97', resource.id, -1)

结果:

 Volkert    
 Volkert    
 Vogel  
 Vogel  
 Vasiliadou 
 Vasiliadou 
 Von Aesch  DGI-PVB
 Von Aesch  DGI-PFR
 Von Aesch  DGI-PBD
 Von Aesch  DGI-PBP

2 个答案:

答案 0 :(得分:0)

SELECT a. lastname , a.keywords from
(SELECT 
    (SELECT DISTINCT(SUBSTRING(REPLACE(resource.name, '*Deleted*', '') 
     FROM 1  
     FOR POSITION(',' IN GROUP_CONCAT(REPLACE(resource.name, '*Deleted*', '' )))-1))) AS lastname,
    organisation.keywords AS keywords
FROM 
    obj_resource resource
LEFT JOIN  
    rel_raci resource_raci  ON resource.ID = resource_raci.PERSON_ID
LEFT JOIN 
    obj_resource organisation on  organisation.ID =  resource_raci.OBJ_ID
 -- Gibt nur die markierten Massnahmen aus
WHERE 
    CONTAINS('463bb931:158af3df8f6:1d97', resource.id, -1)) a
group by a.lastname, a.keywords

答案 1 :(得分:0)

DISTINCT可能需要重新定位,因此它会在SELECT之后发生。在问题中,你在子查询中有它:

SELECT DISTINCT
    (SELECT (SUBSTRING(REPLACE(resource.name, '*Deleted*', '') 
     FROM 1  
     FOR POSITION(',' IN GROUP_CONCAT(REPLACE(resource.name, '*Deleted*', '' )))-1))) AS lastname,
    organisation.keywords AS keywords
FROM 
    obj_resource resource
LEFT JOIN  
    rel_raci resource_raci  ON resource.ID = resource_raci.PERSON_ID
LEFT JOIN 
    obj_resource organisation on  organisation.ID =  resource_raci.OBJ_ID
 -- Gibt nur die markierten Massnahmen aus
WHERE 
    CONTAINS('463bb931:158af3df8f6:1d97', resource.id, -1)