使用FOR XML将N行连接在一行中

时间:2014-12-08 13:21:09

标签: sql sql-server sql-server-2008

我正在寻找输出:

|  BOOK | DETAILS                                                           |
|-------|-------------------------------------------------------------------|
| BookA | COD_100,A,mary.jane||COD_101,P,silvia.poff                        |
| BookB | COD_102,A,andrea.maya                                             |

我得到的是所有行的所有结果,而不是:

|  BOOK |  DETAILS                                                          |
|-------|-------------------------------------------------------------------|
| BookA | COD_100,A,mary.jane||COD_101,P,silvia.poff||COD_102,A,andrea.maya |
| BookB | COD_100,A,mary.jane||COD_101,P,silvia.poff||COD_102,A,andrea.maya |

我有一个问题(Looping lines in SQL)几乎相同,但在这种情况下我无法重现它。 Bellow是我用来获取数据的小提琴:

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE books 
  (
    book varchar(10),
    book_group varchar(10),
    status varchar(10)
  );

INSERT INTO books (book, book_group, status)
VALUES
('BookA', 'A', 'open'),
('BookB', 'A', 'open'),
('BookC', 'B', 'open'),
('BookD', 'C', 'open'),
('BookE', 'D', 'open');

CREATE TABLE books_underanalisys 
  (
    book varchar(10)
  );

INSERT INTO books_underanalisys (book)
VALUES
('BookA'),
('BookB'),
('BookC'),
('BookD'),
('BookE');

CREATE TABLE books_underanalisys_PlanA
  (
    book varchar(10),
    ID_books_underanalisys_PlanA varchar(10)
  );

INSERT INTO books_underanalisys_PlanA (book, ID_books_underanalisys_PlanA)
VALUES
('BookA', '100'),
('BookA', '101'),
('BookB', '102'),
('BookC', '103'),
('BookC', '104');


CREATE TABLE books_underanalisys_PlanA_detail 
  (
    ID_books_underanalisys_PlanA_detail varchar(10),
    COD_books_underanalisys_PlanA_detail varchar(10),
    status varchar(1),
    username varchar(100)
  );

INSERT INTO books_underanalisys_PlanA_detail (ID_books_underanalisys_PlanA_detail, COD_books_underanalisys_PlanA_detail, status, username)
VALUES
('100', 'COD_100', 'A', 'mary.jane'),
('101', 'COD_101', 'P', 'silvia.poff'),
('102', 'COD_102', 'A', 'andrea.maya'),
('103', 'COD_103', 'A', 'johan.kin'),
('104', 'COD_104', 'P', 'hingo.trunk');

查询1

SELECT
    books_underanalisys .book AS Book,
    STUFF((
SELECT
    '||' + books_underanalisys_PlanA_detail.COD_books_underanalisys_PlanA_detail + ',' + books_underanalisys_PlanA_detail.status + ',' + books_underanalisys_PlanA_detail.username AS [text()]
FROM
    books_underanalisys_PlanA right join books_underanalisys_PlanA_detail on books_underanalisys_PlanA.ID_books_underanalisys_PlanA = books_underanalisys_PlanA_detail.ID_books_underanalisys_PlanA_detail,
    books_underanalisys ,
    books
WHERE
    books_underanalisys_PlanA.book = books_underanalisys .book
AND books_underanalisys_PlanA.book = books.book
AND books.book_group = 'A'
AND books.status <> 'closed'
FOR XML PATH('')), 1, 2, '' ) AS DETAILS
FROM
    books_underanalisys_PlanA right join books_underanalisys_PlanA_detail on books_underanalisys_PlanA.ID_books_underanalisys_PlanA = books_underanalisys_PlanA_detail.ID_books_underanalisys_PlanA_detail,
    books_underanalisys ,
    books
WHERE
    books_underanalisys_PlanA.book = books_underanalisys .book
AND books_underanalisys_PlanA.book = books.book
AND books.book_group = 'A'
AND books.status <> 'closed'
GROUP BY
    books_underanalisys .book

Results

|  BOOK |                                                           DETAILS |
|-------|-------------------------------------------------------------------|
| BookA | COD_100,A,mary.jane||COD_101,P,silvia.poff||COD_102,A,andrea.maya |
| BookB | COD_100,A,mary.jane||COD_101,P,silvia.poff||COD_102,A,andrea.maya |

1 个答案:

答案 0 :(得分:3)

看起来你需要这样的东西:

select b.book,
       (
       select '||'+d.COD_books_underanalisys_PlanA_detail+','+d.status+','+d.username
       from books_underanalisys_PlanA as a
         inner join books_underanalisys_PlanA_detail as d
           on a.ID_books_underanalisys_PlanA = d.ID_books_underanalisys_PlanA_detail
       where a.book = b.book
       for xml path(''), type
       ).value('substring(text()[1], 3)', 'varchar(max)') as details
from books as b
where b.book_group = 'A' and
      b.status <> 'closed'