0

我正在使用这个 MS SQL 脚本:

     select NumberId, ART_No, InkColor 
     from INK_STOCK

我有这样的输出:

     NumberId         ART_No          InkColor
     ------------------------------------------
     0010             23003             BLUE
     0010             23003             RED
     0010             23003             GREEN
     0013             23004             ORANGE
     0013             23004             PINK
     0013             23004             WHITE
     0015             23007             GREEN
     0015             23007             PINK

我怎样才能使输出应该是这样的:

     NumberId         ART_No          InkColor
     ------------------------------------------
     0010             23003             BLUE, RED, GREEN
     0013             23004             ORANGE, PINK, WHITE
     0015             23007             GREEN, PINK

谢谢

4

1 回答 1

5

询问:

SQLFIDDLE示例

select c1.NumberId, c1.ART_No,
    STUFF((   SELECT ', ' +x.InkColor
        FROM INK_STOCK x
        WHERE c1.NumberId = x.NumberId
        FOR XML PATH ('')
    ),1,1,'') as InkColor
from INK_STOCK c1
group by c1.NumberId, c1.ART_No

结果:

| NUMBERID | ART_NO |             INKCOLOR |
--------------------------------------------
|       10 |  23003 |     BLUE, RED, GREEN |
|       13 |  23004 |  ORANGE, PINK, WHITE |
|       15 |  23007 |          GREEN, PINK |
于 2013-01-23T10:39:09.017 回答