2

I have a requirement wherein I have 3 tables

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

I need a query that gives me a result like this

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

This is what I have till now !!

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>>'
1
  • Your query and your table definitions do not match? Which are correct? Commented Feb 26, 2013 at 16:03

3 Answers 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
Sign up to request clarification or add additional context in comments.

Comments

1

I Would do somethng like this, where you use the ID of the main QUERY into your 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

This will give you all the role for one Compagnie. If the role does'nt exist you will have a NULL value.

Comments

1

You can use CROSS APPLY to get the result:

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)

See SQL Fiddle with Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.