By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table yourtable (country varchar(100), value varchar(100));
insert into yourtable values ('FR',NULL);
insert into yourtable values ('FR',1);
insert into yourtable values ('FR',3);
insert into yourtable values ('MA',5);
insert into yourtable values ('MA',NULL);
insert into yourtable values ('MA',4);
insert into yourtable values ('ES',9);
insert into yourtable values ('ES',10);
insert into yourtable values ('ES',NULL);
9 rows affected
SELECT country,
val =
STUFF (
(SELECT
',' +value
FROM yourtable
WHERE value IS NOT NULL AND y1.country = country
FOR XML PATH('')
), 1, 1, ''
)
FROM yourtable y1
GROUP by country
ORDER BY country
country | val |
---|---|
ES | 9,10 |
FR | 1,3 |
MA | 5,4 |