Get unique values using STRING_AGG in SQL Server

后端 未结 4 1278
情话喂你
情话喂你 2020-12-20 11:55

The following query returns the results shown below:

SELECT 
    ProjectID, newID.value
FROM 
    [dbo].[Data] WITH(NOLOCK)  
CROSS APPLY 
    STRING_SPLIT([         


        
相关标签:
4条回答
  • 2020-12-20 12:06

    You can make a distinct view of the table, that holds the aggregate values, that is even simpler:

    Create Table Test (field1 varchar(1), field2 varchar(1));
    
    go
    
    Create View DistinctTest as (Select distinct field1, field2 from test group by field1,field2);
    
    go
    
    insert into Test Select 'A', '1';
    insert into Test Select 'A', '2';
    insert into Test Select 'A', '2';
    insert into Test Select 'A', '2';
    insert into Test Select 'D', '1';
    insert into Test Select 'D', '1';
    
    select string_agg(field1, ',')  from Test where field2 = '1';  /* duplicates: A,D,D */;
    
    select string_agg(field1, ',')  from DistinctTest where field2 = '1';  /* no duplicates: A,D  */;
    
    0 讨论(0)
  • 2020-12-20 12:08

    As @SeanLange pointed out in the comments, this is a terrible way to pull out the data, but if you had to, just make it 2 separate queries as follows:

    SELECT 
        ProjectID
        ,STRING_AGG( val, ',') WITHIN GROUP (ORDER BY val) AS NewField
    FROM
    (
        SELECT DISTINCT 
            ProjectID
            ,newID.value AS val
        FROM 
            [dbo].[Data] WITH(NOLOCK)  
            CROSS APPLY STRING_SPLIT([bID],';') AS newID  
        WHERE 
            newID.value IN ('O95833' , 'Q96NY7-2') 
    ) t
    GROUP BY
        ProjectID
    

    That should do it.

    0 讨论(0)
  • 2020-12-20 12:14

    Use the DISTINCT keyword in a subquery to remove duplicates before combining the results: SQL Fiddle

    SELECT 
    ProjectID
    ,STRING_AGG(value, ',') WITHIN GROUP (ORDER BY value) AS 
    NewField
    from (
        select distinct ProjectId, newId.value 
        FROM [dbo].[Data] WITH(NOLOCK)  
        CROSS APPLY STRING_SPLIT([bID],';') AS newID  
        WHERE newID.value IN (   'O95833' , 'Q96NY7-2'  )  
    ) x
    GROUP BY ProjectID
    ORDER BY ProjectID
    
    0 讨论(0)
  • 2020-12-20 12:17

    You can use distinct in the subquery used for the apply:

    SELECT d.ProjectID,
           STRING_AGG(  newID.value, ',') WITHIN GROUP (ORDER BY newID.value) AS 
    NewField
    FROM [dbo].[Data] d CROSS APPLY
         (select distinct value
          from STRING_SPLIT(d.[bID], ';') AS newID 
         ) newID
    WHERE newID.value IN (   'O95833' , 'Q96NY7-2'  ) 
    group by projectid;
    
    0 讨论(0)
提交回复
热议问题