Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unique values using STRING_AGG in SQL Server

The following query returns the results shown below:

SELECT      ProjectID, newID.value FROM      [dbo].[Data] WITH(NOLOCK)   CROSS APPLY      STRING_SPLIT([bID],';') AS newID   WHERE      newID.value IN ('O95833', 'Q96NY7-2')  

Results:

ProjectID   value --------------------- 2           Q96NY7-2 2           O95833 2           O95833 2           Q96NY7-2 2           O95833 2           Q96NY7-2 4           Q96NY7-2 4           Q96NY7-2 

Using the newly added STRING_AGG function (in SQL Server 2017) as it is shown in the following query I am able to get the result-set below.

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

Results:

ProjectID   NewField ------------------------------------------------------------- 2           O95833,O95833,O95833,Q96NY7-2,Q96NY7-2,Q96NY7-2 4           Q96NY7-2,Q96NY7-2 

I would like my final output to have only unique elements as below:

ProjectID   NewField ------------------------------- 2           O95833, Q96NY7-2 4           Q96NY7-2 

Any suggestions about how to get this result? Please feel free to refine/redesign from scratch my query if needed.

like image 379
gkoul Avatar asked May 29 '18 16:05

gkoul


People also ask

How do I get unique values in SQL Server?

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

What is String_agg in SQL Server?

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions.


2 Answers

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 
like image 147
JohnLBevan Avatar answered Sep 20 '22 20:09

JohnLBevan


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; 
like image 40
Gordon Linoff Avatar answered Sep 16 '22 20:09

Gordon Linoff