web-dev-qa-db-fra.com

Puis-je délimiter par virgule plusieurs lignes dans une seule colonne?

J'essaie de fusionner quelque chose comme ça dans ma base de données SQL Server:

[TicketID], [Personne] 
 T0001 Alice 
 T0001 Bob 
 T0002 Catherine 
 T0002 Doug 
 T0003 Elaine

En cela:

[TicketID], [People] 
 T0001 Alice, Bob 
 T0002 Catherine, Doug 
 T0003 Elaine

Je dois le faire à la fois dans SQL Server et Oracle.

J'ai trouvé la fonction GROUP_CONCAT pour MySQL qui fait exactement ce dont j'ai besoin ici, mais MySQL n'est pas une option ici.

EDIT: Banc d'essai:

DECLARE @Tickets TABLE (
    [TicketID] char(5) NOT NULL,
    [Person] nvarchar(15) NOT NULL
)

INSERT INTO @Tickets VALUES
    ('T0001', 'Alice'),
    ('T0001', 'Bob'),
    ('T0002', 'Catherine'),
    ('T0002', 'Doug'),
    ('T0003', 'Elaine')

SELECT * FROM @Tickets
67
John Gietzen

Voici une solution qui fonctionne dans SQL Server 2005+:

SELECT t.TicketID,
       STUFF(ISNULL((SELECT ', ' + x.Person
                FROM @Tickets x
               WHERE x.TicketID = t.TicketID
            GROUP BY x.Person
             FOR XML PATH (''), TYPE).value('.','VARCHAR(max)'), ''), 1, 2, '') [No Preceeding Comma],
       ISNULL((SELECT ', ' + x.Person
                FROM @Tickets x
               WHERE x.TicketID = t.TicketID
            GROUP BY x.Person
             FOR XML PATH (''), TYPE).value('.','VARCHAR(max)'), '') [Preceeding Comma If Not Empty]
  FROM @Tickets t
GROUP BY t.TicketID

Référence:

73
OMG Ponies

Et, la version MySQL, pour être complet:

select
    TicketId,
    GROUP_CONCAT(Person ORDER BY Person SEPARATOR ', ') People
from
    table
group by
    TicketId
13
John Gietzen
DECLARE @Tickets TABLE (
    [TicketID] char(5) NOT NULL,
    [Person] nvarchar(15) NOT NULL
)
INSERT INTO @Tickets VALUES
    ('T0001', 'Alice'),
    ('T0001', 'Bob'),
    ('T0002', 'Catherine'),
    ('T0002', 'Doug'),
    ('T0003', 'Elaine')

SELECT * FROM @Tickets

Select [TicketID],
STUFF((SELECT ',' + Person FROM @Tickets WHERE (
TicketID=Result.TicketID) FOR XML PATH ('')),1,1,'') AS BATCHNOLIST
From @Tickets AS Result
GROUP BY TicketID
11
satish

J'ai trouvé un moyen de le faire dans Oracle, mais je dois encore le faire dans SQL Server.

De http://technology.amis.nl/blog/6118/Oracle-rdbms-11gr2-listagg-new-aggregation-operator-for-creating-comma-delimited-strings (Merci tanging ) (Oracle 11 et supérieur)

select
    TicketId,
    listagg(Person, ', ') People
from
    table
group by
    TicketId

De: http://halisway.blogspot.com/2006/08/Oracle-groupconcat-updated-again.html

with
    data
as
  (
    select
        TicketId,
        Person,
        ROW_NUMBER() over (partition by TicketId order by Person) "rownum",
        COUNT(*) over (partition by TicketId) "count"
    from
        Table
  )
select
    TicketId,
    LTRIM(sys_connect_by_path(Person,','),',') People
from
    data
where
    "rownum" = "count"
start with
    "rownum" = 1
connect by
    prior TicketId = TicketId
  and
    prior "rownum" = "rownum" - 1
order by
    TicketId
10
John Gietzen

un exemple

SELECT DISTINCT
    t.TicketID,
    STUFF((SELECT ', ', i.Person as [text()]
           FROM @Tickets i 
           WHERE i.TicketID = t.TicketID
           FOR XML PATH ('')), 1, 2, '') as People
FROM
    @Tickets t

......... ou essayez ..............

SELECT DISTINCT
    t.TicketID,
    STUFF((SELECT ', ' + i.Person    /* notice this line is different */
           FROM @Tickets i 
           WHERE i.TicketID = t.TicketID
           FOR XML PATH ('')), 1, 2, '') as People
FROM
    @Tickets t

/ * cela fonctionne quand je l'ai utilisé pour ma table et le mérite revient à mon manager qui ROCKS! * /

8
Nishad M