Combinar varias filas en una columna sin duplicados


Estoy trabajando en una consulta que recopilará datos de una tabla y mostrará los datos para un informe.

Los datos se ven así:

Player Score
001      10
001      20
002      20
002      20
001      10
002      10
003      20
002      20
001      10

Quiero que lo muestre así

Player Score
001    10,20
002    10,20
003    20

Pero todo lo que consigo es una lista combinada de todos los datos en la columna de puntuación como esta

Player Score
001    10,20,10,10
002    20,20,10,20
003    20

¿Alguien tiene una idea de cómo hacer que esto funcione?

Author: Taryn, 2012-09-28

3 answers

Para SQL Server puede usar:

select player,
  stuff((SELECT distinct ', ' + cast(score as varchar(10))
           FROM yourtable t2
           where t2.player = t1.player
           FOR XML PATH('')),1,1,'') 
from yourtable t1
group by player
 37
Author: Taryn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-19 17:06:24

Un poco tarde y un poco fuera de tema como para otro RDBMS, pero encontré este hilo buscando una solución a este problema en Postgres. Encontré uno, así que si alguien más necesita resolver este problema en Pg:

SELECT string_agg(DISTINCT <column>,'delimiter') FROM <table> GROUP BY <column2>
 1
Author: dermesser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-17 10:34:20

ACTUALIZAR AllNews SET ArticleSource = pp.[NewsText] DE AllNews COMO un UNIÓN INTERIOR (seleccionar t1.Id, cosas((SELECT distinct '."+t2.[Texto] DE NewsPhotos t2 donde t2.NewsId = t1.Id PARA RUTA XML(")),1,1,") como [NewsText] de AllNews t1 agrupar por t1.Id) como pp ON pp.Id = an.Id

 -2
Author: Tigran,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-01 08:12:29