0

一笔付款可以有多个发票,因此查询表或结果集可能会带回多行。

我该如何阻止这个?

Customer Name   Payment no   Invoice No
-------------   ----------   ----------    
Dave A          1            10
Dave T          2            10
Dave T          2            12

所以我想要:

Customer Name   Payment no   Invoice No
-------------   ----------   ----------    
Dave A          1            10
Dave T          2            10, 12
4

1 回答 1

3
SELECT
     [Customer Name], [Payment no],
     STUFF(
         (SELECT ',' + CAST([Invoice No] AS VARCHAR(10))
          FROM TableName
          WHERE [Customer Name] = a.[Customer Name] AND
                [Payment no] = A.[Payment no]
          FOR XML PATH (''))
          , 1, 1, '')  AS InvoiceList
FROM TableName AS a
GROUP BY [Customer Name], [Payment no]
于 2013-03-07T15:15:39.803 回答