t-sql查询连接相应的行

时间:2011-06-02 16:45:08

标签: sql-server tsql sql-server-2008-r2

我有一张桌子Managers。专栏:ManagerId, ManagerName

我有一张桌子Clients。专栏:ClientId, ManagerId, ClientName, ClientAddress, Details

每个(每个?)经理在客户表中都有客户。

我需要一个返回下一个表的查询。列:Column1,Column2。

第1列:ManagerName

第2栏:ClientName1 ',' ClientAddress1 ',' Details1 ';' ClientName2 ',' ClientAddress2 ',' Details2 ';' etc

在column2中只是一个与column1中的manager相对应的客户端列表。

我该怎么做?

我想我需要使用COALESCE,但我不确定。

3 个答案:

答案 0 :(得分:2)

与之前的答案相同,只是处理空值和XML特殊字符<>&'"

declare @Managers table
(
  ManagerId int,
  ManagerName varchar(50)
)

declare @Clients table
(
  ClientId int,
  ManagerId int,
  ClientName varchar(50),
  ClientAddress varchar(50),
  Details varchar(50)
)

insert into @Managers values(1, 'Manager 1')
insert into @Managers values(2, 'Manager 2')

insert into @Clients values (1, 1, 'Client 1',   'CA 1', 'D 1')
insert into @Clients values (2, 1, 'Client 2',   'CA 2', 'D 2')
insert into @Clients values (3, 2, 'XML special characters &<>" and null values', null,  'D 3')

select M.ManagerName,
       stuff((select '; '+
                     coalesce(C.ClientName, '')+', '+
                     coalesce(C.ClientAddress, '')+', '+
                     coalesce(C.Details, '')
              from @Clients as C 
              where C.ManagerId = M.ManagerId
              for xml path(''), type).value('.', 'nvarchar(max)'), 1, 2, '') as Clients
from @Managers as M

结果:

ManagerName  Clients
-----------  ---------------------------------------------------
Manager 1    Client 1, CA 1, D 1; Client 2, CA 2, D 2
Manager 2    XML special characters &<>" and null values, , D 3

答案 1 :(得分:1)

以下是使用XML AUTO

的工作示例
With Managers AS
(
      Select 1 as ManagerId, 'Tom' as ManagerName
UNION Select 2 as ManagerId, 'Jane' as ManagerName
),
Clients as
(
      Select 1 as ClientId, 1 as ManagerId, 'TaXon Pro' as ClientName, '112 Acme St Cityville DD 56' as ClientAddress, 'ABC' as Details
UNION Select 2 as ClientId, 1 as ManagerId, 'Pro Xon' as ClientName, '12342 Bylo Rd Streetville DD 156' as ClientAddress, 'CDR' as Details
UNION Select 3 as ClientId, 1 as ManagerId, 'Clean Svc' as ClientName, '6512 Toni St Townville DD 1236' as ClientAddress, 'D@#$' as Details
UNION Select 4 as ClientId, 2 as ManagerId, 'ContraRel' as ClientName, '152 Acme St Villageville DD 3456' as ClientAddress, 'SER' as Details
UNION Select 5 as ClientId, 2 as ManagerId, 'RepoIn' as ClientName, '1 Acme St Districtville DD 1456' as ClientAddress, 'KH' as Details
)
SELECT 
    M.ManagerName,
    (
        SELECT c.ClientName + ', ' + C.ClientAddress + ', ' + c.Details + ';'
        FROM Clients C
        WHERE m.ManagerId = c.ManagerId
        FOR XML PATH('') 
    ) AS Clients
FROM Managers M
GROUP BY 
    m.ManagerId, M.ManagerName

输出将如下所示

ManagerName Clients
----------- -------------------------------------------------------------------------------------------------------------------------------------------
Tom         TaXon Pro, 112 Acme St Cityville DD 56, ABC;Pro Xon, 12342 Bylo Rd Streetville DD 156, CDR;Clean Svc, 6512 Toni St Townville DD 1236, D@#$;
Jane        ContraRel, 152 Acme St Villageville DD 3456, SER;RepoIn, 1 Acme St Districtville DD 1456, KH;

(2 row(s) affected)

答案 2 :(得分:1)

SELECT 
    ManagerName,
    (
        SELECT ClientName + ', ' + ClientAddress + ', ' + Details + ' ; '
        FROM Clients c
        WHERE c.ManagerId = m.ManagerId
        FOR XML PATH ('')
    ) AS clients
FROM Managers m