带逗号分隔值的SQL查询

时间:2013-02-26 15:54:47

标签: sql sql-server tsql

我有一个要求,其中我有3个表

ContactInfo

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

ContactRole

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>>'

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

我会做这样的事情,在那里你将主QUERY的ID用到你的SUBQUERY

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

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

答案 2 :(得分: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 with Demo