2

我有一个要求,其中我有 3 张桌子

联系方式

idContactinfo  FirstName  Lastname      idCOmpanyid
---------------------------------------------------
1               Blah1      Blah1            1
2               Blah2      Blah2            1
3               Blah3      Blah3            1
4               Blah4      Blah4            1  

联系人角色

idcontactrole     IdContactRoleName      idContactInfo   
------------------------------------------------------
1                       1                     1
2                       1                     2
3                       1                     3
4                       2                     4

IdContactRoleLookup

idcontactRole           RoleName
----------------------------------
1                        Admin
2                        Secretary

我需要一个给我这样的结果的查询

idcontactrolename            Contacts
--------------------------------------------------------
Admin                    Blah1,Blah1;Blah2,Blah2,Blah3,Blah3
Secretary                Blah4,Blah4

这是我到现在为止的!

SELECT l.sValue
    , SUBSTRING((
                        SELECT C.sLast + ',' + C.sFirst + ';'
                        FROM contactinfo c
                        inner join contactrole crole
                        ON crole.idcontactinfo = c.idcontactinfo
                        WHERE crole.idcompany = '<<blah>>'
                        and c.idcompany = '<<blah>>'
                        FOR XML PATH('')

                    ), 1, 1000000)
FROM CONTACTROLE CR
inner join contactinfo c on 
cr.idcontactinfo = c.idcontactinfo 
inner join lookupdata l on cr.idlookuprole = l.idlookupdata
where c.idcompany = '<<blah>>'
4

3 回答 3

2
SELECT
     RoleName,
     STUFF(
         (
            SELECT  ';' + b.FirstName + ', ' + b.LastName
            FROM    ContactRole a
            INNER JOIN ContactInfo b
                  ON a.idContactInfo = b.idContactinfo
            WHERE   a.IdContactRoleName = r.idcontactRole
            FOR XML PATH (''))
            , 1, 1, '')  AS NamesList
FROM  IdContactRoleLookup r
GROUP BY idcontactRole, RoleName
于 2013-02-26T16:05:13.890 回答
1

我会做这样的事情,您将主查询的 ID 用于您的子查询

SELECT     
l.sValue
    , SUBSTRING((
                        SELECT C.sLast + ',' + C.sFirst + ';'
                        FROM contactinfo c
                        inner join contactrole crole
                        ON crole.idcontactinfo = c.idcontactinfo
                        WHERE c.idcompany = '<<blah>>'
AND c.idcontactrole = CR.idcontactrole
                        FOR XML PATH('')

                    ), 1, 1000000)

FROM CONTACTROLE CR
inner join lookupdata l on cr.idlookuprole = l.idlookupdata

这将为您提供一个公司的所有角色。如果角色不存在,您将有一个 NULL 值。

于 2013-02-26T16:03:27.627 回答
1

您可以使用CROSS APPLY来获得结果:

select distinct l.RoleName,
  left(list, len(list) -1) list
FROM CONTACTROLE CR
inner join IdContactRoleLookup l 
  on cr.IdContactRoleName = l.idcontactRole
cross apply
(
  select C1.Lastname + ',' + C1.FirstName + ';'
  from CONTACTROLE CR1
  inner join contactinfo c1
    on cr1.idContactInfo = c1.idContactinfo
  where cr.IdContactRoleName = cr1.IdContactRoleName
  FOR XML PATH('')
) t2 (list)

请参阅带有演示的 SQL Fiddle

于 2013-02-26T16:09:37.490 回答