SQL: Compare and gathering data

I have a sql table that contains user names and the agencies they are restricted to:
UserName  Agency
Dave           T200
MMein          04042
Dave            HHF001
LSand          HCF200

I want to return data from that table looking like this:
Dave          T200, HHF001
MMein         04042
LSand         HCF200

So far my sproc looks like this.  However, it brings back all of the agencies into one user name, like this:
Dave     T200, HHF001, 04042, HCF200
What is the proper way to accomplish this?  Thanks

DECLARE
@userID                        INT = 42
,@userName                  VARCHAR(25)
,@agencyList            VARCHAR(100)
,@userAgencyList      VARCHAR(100)

SET @userAgencyList = ''
SET @agencyList = ''

select
@userName = u.DisplayName,
@userAgencyList  = @userAgencyList + a.Identifier + ' '
from cad.UserAgencyRestriction uar
left join adm.[User] u
      on u.UserID = uar.UserID
left  join adm.Agency  a
      on a.AgencyID = uar.AgencyID
MEINMELAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jeyakkumarCommented:
HI,
 The below query will work for MySQL ..

SELECT username, group_concat(Agency)
as data FROM <tablename> group by username

Hope it will help you (MySQL it is very simple)
0
jeyakkumarCommented:
Please try this in SQL Server

SELECT
distinct username
,NameList=STUFF((SELECT ','+agency FROM <tablename> WHERE username=A.username FOR XML PATH('')) , 1 , 1 , '' )
FROM
<tablename> A
order by 1 desc

Hope it may work .. :)
0
LowfatspreadCommented:
like this
select 
 u.DisplayName,
Stuff((
SELECT ' '+a.identifier
FROM adm.Agency  a
WHERE a.AgencyID = uar.AgencyID
FOR XML PATH(”)),1,1,'') as Agencylist

from cad.UserAgencyRestriction uar
left join adm.[User] u
      on u.UserID = uar.UserID
order by 1

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

MEINMELAuthor Commented:
Wow, thanks for the responses!
This is what I've got now:
 
select
  u.DisplayName,
  STUFF((select ',' + a.identifier From adm.agency a where
                  a.AgencyID = uar.AgencyID FOR XML Path('')),1,1,'') as AgencyList
from
   cad.UserAgencyRestriction uar
   left join adm.[User] u
     on u.UserID = uar.UserID
ORDER by 1

However, it is returning each as a seperate row:

LSand    04042
Lsand    T200
MMein    MDF200
MMein    HHF001

and I would like them like this:

LSand    04042 T200 (or comma instead of space)
MMein    MDF200 HHF001
0
LowfatspreadCommented:
select
  u.DisplayName,
  STUFF((select ',' + a.identifier
                From cad.UserAgencyRestriction uar
                inner join adm.agency a
                 on a.AgencyID = uar.AgencyID
                where  u.UserID = uar.UserID
                FOR XML Path('')),1,1,'') as AgencyList
from    
  adm.[User] u
     
ORDER by 1
0
MEINMELAuthor Commented:
Getting closer.  However, the returned list includes all DisplayNames from adm.User with AgencyList NULL for most - except the ones that actually have the restrictions - and those are displaying as I need them to.
0
LowfatspreadCommented:

select
  u.DisplayName,
  STUFF((select ',' + a.identifier
                From cad.UserAgencyRestriction uar
                inner join adm.agency a
                 on a.AgencyID = uar.AgencyID
                where  u.UserID = uar.UserID
                FOR XML Path('')),1,1,'') as AgencyList
from  (select distinct userid
         from  cad.UserAgencyRestriction) as uar
inner join adm.[User] u
on u.userid=uar.userid
     
ORDER by 1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MEINMELAuthor Commented:
YOU ROCK!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.