Solved

How to identify what are the employees has same Roles

Posted on 2014-01-05
2
341 Views
Last Modified: 2014-01-05
I've table as given below. One employee can be assigned to multiple roles. I would like to identify what users has the same set of roles.

Ex

EmpID RoleID

1           2
1           3
4           2
4           3
5           1

I above example, Employees 1 and  4 has same set of roles. Hence I would like to get the result as (1,4).

How to achieve this using query. Please do assist.
0
Comment
Question by:Easwaran Paramasivam
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
You need to use FOR XML PATH, like this:
;WITH r AS (
SELECT EmpID, 
    (STUFF((SELECT DISTINCT ',' + CONVERT(VARCHAR(100),P1.RoleID) AS [text()]
            FROM Table1 P1
            WHERE P1.EmpID = P.EmpID 
            FOR XML PATH('')), 1, 1, '')) AS Roles
FROM Table1 P
GROUP BY EmpID)
SELECT Roles, 
    (STUFF((SELECT DISTINCT ',' + CONVERT(VARCHAR(100),r1.EmpID) AS [text()]
            FROM r r1
            WHERE r1.Roles = r.Roles 
            FOR XML PATH('')), 1, 1, '')) AS Empls
FROM r
GROUP BY Roles
HAVING COUNT(*) > 1 -- comment this line if you want to include the sets with employees for whom no matching pairs found

Open in new window

SQL Fiddle
0
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
Comment Utility
Brilliant!!!!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now