Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Gabungkan beberapa baris data menjadi satu baris

Anda dapat menggunakan STUFF untuk menghasilkan nilai csv. Di bawah ini adalah apa yang biasanya saya gunakan.

    CREATE TABLE #Book
    (
        Id INT ,
        NAME VARCHAR(100) ,
        Description VARCHAR(100)
    );

CREATE TABLE #BookReader
    (
        BookId INT ,
        Name VARCHAR(100)
    );


CREATE TABLE #BookVersion
    (
        BookId INT ,
        Version VARCHAR(10)
    );


INSERT INTO #Book ( Id ,
                    NAME ,
                    Description )
VALUES ( 1 ,            -- Id - int
         'Book1' ,      -- NAME - varchar(100)
         'Book 1 Title' -- Description - varchar(100)
    ) ,
       ( 2 ,            -- Id - int
         'Book2' ,      -- NAME - varchar(100)
         'Book 2 Title' -- Description - varchar(100)
       ) ,
       ( 3 ,            -- Id - int
         'Book3' ,      -- NAME - varchar(100)
         'Book 3 Title' -- Description - varchar(100)
       ) ,
       ( 4 ,            -- Id - int
         'Book5' ,      -- NAME - varchar(100)
         'Book 5 Title' -- Description - varchar(100)
       );

INSERT INTO #BookReader ( BookId ,
                          Name )
VALUES ( 1 ,     -- BookId - int
         'James' -- Name - varchar(100)
    ) ,
       ( 2 ,        -- BookId - int
         'Stephane' -- Name - varchar(100)
       ) ,
       ( 2 ,       -- BookId - int
         'Michael' -- Name - varchar(100)
       );

INSERT INTO #BookVersion ( BookId ,
                           Version )
VALUES ( 1 ,  -- BookId - int
         'v1' -- Version - varchar(10)
    ) ,
       ( 1 ,  -- BookId - int
         'v2' -- Version - varchar(10)
       ) ,
       ( 2 ,  -- BookId - int
         'v1' -- Version - varchar(10)
       ) ,
       ( 2 ,  -- BookId - int
         'v2' -- Version - varchar(10)
       ) ,
       ( 2 ,  -- BookId - int
         'v3' -- Version - varchar(10)
       );


SELECT b.Id ,
       LTRIM(STUFF((   SELECT ', ' + br1.[Name] AS [text()]
                 FROM   #BookReader br1
                 WHERE  br1.BookId = b.Id
                 FOR XML PATH('')) ,
             1 ,
             1 ,
             '')) AS Reader ,
       LTRIM(STUFF((   SELECT ', ' + Version
                 FROM   #BookVersion bv1
                 WHERE  bv1.BookId = b.Id
                 FOR XML PATH('')) ,
             1 ,
             1 ,
             '')) AS version
FROM   #Book b;



DROP TABLE #Book;
DROP TABLE #BookReader;
DROP TABLE #BookVersion;

Hasil:

+----+--------------------+-------------+
| Id |       Reader       |   Version   |
+----+--------------------+-------------+
|  1 | James              | v1, v2      |
|  2 | Stephane, Michael  | v1, v2, v3  |
|  3 | NULL               | NULL        |
|  4 | NULL               | NULL        |
+----+--------------------+-------------+



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. TSQL Bagaimana memilih karyawan dengan keterampilan di kolom xml

  2. Otomatiskan Pengambilan nomor Versi dari file .Dtsx

  3. Beberapa klausa OUTPUT dalam perintah SQL MERGE/INSERT/DELETE?

  4. Pemicu untuk memecat hanya jika suatu kondisi terpenuhi di SQL Server

  5. PDO DBLIB mengakses SQL Server 2008 dan 2012