question about regrouping the rows

hi all,

I have a similar issue posted here, but not exactly the same.

I have a table
emp_dept
(emp_id numeric,
dept_id numeric,
department char(3) )

employees
(emp_id numeric,
 emp_name varchar(100)
)

1: 1 employee can belong to 2 different departments
2: I want the output like
emp id, emp_name, (list of all departments the emp_id belongs to )--separated by comma)

this is for displaying, I know we can use a temp table etc to do this, but was looking for any better approach.
soujanya nomulaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

PortletPaulEE Topic AdvisorCommented:
SELECT
  e.emp_id
, e.emp_name
, ca1.departments
FROM [employees] AS e
    CROSS APPLY (
        SELECT
        STUFF((
              SELECT
                    ', ' + d.department
              FROM [emp_dept] AS d
              WHERE d.emp_id = e.emp_id
              ORDER BY  d.department --<< if wanted
              FOR XML PATH ('')
              )
             , 1, 1, '')
         ) ca1 (departments)
;

Open in new window

{+edit} added: order by
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
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

From novice to tech pro — start learning today.