Return Product Column using T-SQL 所有不同的值都应该是不同的
Return all the distinct values of Product Column using T-SQL all values should be Distinct
采取以下table:
ID
Products
12
xx,yy,xx
13
yy,xx,yy
14
tt,xx,tt
15
yy,yy,yy
我需要一个 T-SQL 来在产品列中给我不同的值
所需结果如下:
ID
Products
12
xx,yy
13
yy,xx
14
tt,xx
15
yy
使用STRING_SPLIT
、distinct
和string_agg
如下
SELECT ID,
String_agg(value, ',') AS Products
FROM (SELECT DISTINCT ID,
value
FROM (SELECT ID,
a.value
FROM table
CROSS apply String_split(products, ',') a) b) c
GROUP BY ID
采取以下table:
ID | Products |
---|---|
12 | xx,yy,xx |
13 | yy,xx,yy |
14 | tt,xx,tt |
15 | yy,yy,yy |
我需要一个 T-SQL 来在产品列中给我不同的值
所需结果如下:
ID | Products |
---|---|
12 | xx,yy |
13 | yy,xx |
14 | tt,xx |
15 | yy |
使用STRING_SPLIT
、distinct
和string_agg
如下
SELECT ID,
String_agg(value, ',') AS Products
FROM (SELECT DISTINCT ID,
value
FROM (SELECT ID,
a.value
FROM table
CROSS apply String_split(products, ',') a) b) c
GROUP BY ID