sql-server: how can I list distinct value of table in a single row, separated by comma
Posted
by RedsDevils
on Stack Overflow
See other posts from Stack Overflow
or by RedsDevils
Published on 2010-04-09T08:39:02Z
Indexed on
2010/04/09
8:43 UTC
Read the original article
Hit count: 388
I have the following Table:
CREATE TABLE TEMP (ID INT,SEGMENT CHAR(1),SEGOFF INT,CHECKED SMALLDATETIME)
INSERT INTO TEMP VALUES (1,'A',0,'2009-05-01')
INSERT INTO TEMP VALUES (2,'B',1,'2009-05-01')
INSERT INTO TEMP VALUES (3,'C',0,'2009-05-01')
INSERT INTO TEMP VALUES (4,'A',0,'2009-05-02')
INSERT INTO TEMP VALUES (5,'B',2,'2009-05-02')
INSERT INTO TEMP VALUES (6,'C',1,'2009-05-02')
INSERT INTO TEMP VALUES (7,'A',1,'2009-05-03')
INSERT INTO TEMP VALUES (8,'B',0,'2009-05-03')
INSERT INTO TEMP VALUES (9,'C',2,'2009-05-03')
I would like to show Distinct SEGEMENT in Single row separated by comma (e.g: A,B,C)
I try as Follow:
DECLARE @SEGMENTList varchar(100)
SELECT @SEGMENTList = COALESCE(@SEGMENTList + ', ', '') +
SEGMENT
FROM TEST
SELECT @SEGMENTList
It shows A, B, C, A, B, C, A, B, C
What do I need to change my query? Thanks all!
© Stack Overflow or respective owner