Solved

How to identify what are the employees has same Roles

Posted on 2014-01-05
2
346 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
ID: 39758415
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
ID: 39758427
Brilliant!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
triggered use of sp_send_dbmail failure 2 22
TSQL DateADD update Question 4 27
TSQL previous 5 23
Anyway to make these 2 SQL statements into one? 13 23
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

21 Experts available now in Live!

Get 1:1 Help Now