4

我想知道将以下行中的数据合并到另一个视图中的单行的最佳方法。

这些是当前显示的结果;

 Type_ID | Client_ID              | PBX_Vendor |
 127     | 090820006311404926326C | Aastra     |
 127     | 090820006311404926326C | Ericsson   |
 127     | 111012237401404926326C | Aastra     |
 127     | 120209287521404926326C | Aastra     |
 127     | 120209287521404926326C | Alcatel    |

以下是我希望如何查看数据;

 Type_ID | Client_ID              | PBX_Vendor       |
 127     | 090820006311404926326C | Aastra, Ericsson |
 127     | 111012237401404926326C | Aastra           |
 127     | 120209287521404926326C | Aastra, Alcatel  |

基本上,有多个 PBX 供应商与一个客户 ID 相关联。对于帮助台系统,我需要在一行中显示此显示。

我已经尝试过使用 CONCAT,但我最终得到的只是一行,其中包含 100 多个供应商,这些供应商并非特定于 Client_ID。

非常感谢任何帮助!

4

2 回答 2

7

这是一种方法(也适用于 2005):

桌子

DECLARE @table TABLE
    (
      [Type_ID] INT,
      [Client_ID] VARCHAR(50),
      [PBX_Vendor] VARCHAR(50)
    )

数据

INSERT  INTO @table
        SELECT  127,
                '090820006311404926326C',
                'Aastra'
        UNION ALL
        SELECT  127,
                '090820006311404926326C',
                'Ericsson'
        UNION ALL
        SELECT  127,
                '111012237401404926326C',
                'Aastra'
        UNION ALL
        SELECT  127,
                '120209287521404926326C',
                'Aastra'
        UNION ALL
        SELECT  127,
                '120209287521404926326C',
                'Alcatel'

询问

SELECT  [Type_ID],
        [Client_ID],
        (
          SELECT    STUFF((
                            SELECT  ',' + [PBX_Vendor]
                            FROM    @table
                            WHERE   [Client_ID] = tbl.[Client_ID]
                                    AND [Type_ID] = tbl.[Type_ID]
                            GROUP BY [PBX_Vendor]
                            ORDER BY [PBX_Vendor]
                          FOR
                            XML PATH('')
                          ), 1, 1, '')
        ) PBX_Vendor
FROM    @table tbl
GROUP BY [Type_ID],
        [Client_ID]

结果

Type_ID     Client_ID               PBX_Vendor
127         090820006311404926326C  Aastra,Ericsson
127         111012237401404926326C  Aastra
127         120209287521404926326C  Aastra,Alcatel
于 2012-05-11T14:32:56.717 回答
0

丹,我已经设法使用您的原始想法进行了一些修改。尽管我无法将其保存为视图,但我了解您无法将 DECLARES 保存为视图;

DECLARE @table TABLE
    (
      [Type_ID] INT,
      [Client_ID] VARCHAR(50),
      [PBX_Vendor] VARCHAR(50)
    )

INSERT  INTO @table
        SELECT  dbo.AMGR_User_Fields_Tbl.Type_Id, dbo.AMGR_User_Fields_Tbl.Client_Id, dbo.AMGR_User_Field_Defs_Tbl.Description AS PBX_Vendor
            FROM    dbo.AMGR_User_Fields_Tbl INNER JOIN
                    dbo.AMGR_User_Field_Defs_Tbl ON dbo.AMGR_User_Fields_Tbl.Type_Id = dbo.AMGR_User_Field_Defs_Tbl.Type_Id AND 
                    dbo.AMGR_User_Fields_Tbl.Code_Id = dbo.AMGR_User_Field_Defs_Tbl.Code_Id
            WHERE     (dbo.AMGR_User_Fields_Tbl.Type_Id = 127)

SELECT  [Type_ID],
        [Client_ID],
        (
          SELECT    STUFF((
                            SELECT  ', ' + [PBX_Vendor]
                            FROM    @table
                            WHERE   [Client_ID] = tbl.[Client_ID]
                                    AND [Type_ID] = tbl.[Type_ID]
                            GROUP BY [PBX_Vendor]
                            ORDER BY [PBX_Vendor]
                          FOR
                            XML PATH('')
                          ), 1, 1, '')
        ) PBX_Vendor
FROM    @table tbl
GROUP BY [Type_ID],
        [Client_ID]
于 2012-05-15T15:42:15.633 回答