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
',' + COALESCE(value,'')
FROM yourtable
WHERE y1.country = country
ORDER BY COALESCE(value,0)
FOR XML PATH('')
), 1, 1, ''
)
FROM yourtable y1
GROUP by country
ORDER BY country
country | val |
---|---|
ES | ,9,10 |
FR | ,1,3 |
MA | ,4,5 |