I am trying to write a SQL script in SSMS 2008 and have been looking around online for an answer and nothing I'm finding has been a complete solution.
I have information across several tables about Roles, Groups, and Profiles that are assigned to a user and a script to get that information. The results I'm getting are "Correct" but not being displayed in the way the customer would like to see them. The Roles, Groups, and Profiles are not related to each other in any way and there may be a different number of each of them assigned to one user (ie 2 roles, 1 group, and 4 profiles).
Here is my SQL script:
select distinct
DWUser.uid as ID,
DWUser.name as Name,
DWRoles.name as Roles,
DWGroup.name as Groups,
DWFCProfile.name as Profiles
from DWUser
/* Joins for getting role name */
inner join DWUserToRole on DWUser.uid = DWUserToRole.uid
inner join DWRoles on DWUserToRole.rid = dwRoles.rid
/* Joins for getting group name */
inner join DWUserToGroup on DWUser.uid = DWUserToGroup.uid
inner join DWGroup on DWUserToGroup.gid = DWGroup.gid
/* Joins for getting profile name */
inner join DWFCProfileToUser on DWUser.uid = DWFCProfileToUser.uid
inner join DWFCProfile on DWFCProfileToUser.fpid = DWFCProfile.fpid
Which returns this result:
ID Name Roles Groups Profiles
1 admin Organization Administrator Public Contracts
1 admin Organization Administrator Public My Basket 1
1 admin Organization Administrator Public My Basket 2
1 admin Organization Administrator Public My Basket 3
1 admin System Administrator Public Contracts
1 admin System Administrator Public My Basket 1
1 admin System Administrator Public My Basket 2
1 admin System Administrator Public My Basket 3
4 docuware Organization Administrator Public Contracts
4 docuware Organization Administrator Public My Basket 1
4 docuware Organization Administrator Public My Basket 2
4 docuware Organization Administrator Public My Basket 3
4 docuware Organization Administrator votosign Contracts
4 docuware Organization Administrator votosign My Basket 1
4 docuware Organization Administrator votosign My Basket 2
4 docuware Organization Administrator votosign My Basket 3
The way the customer wants to see the results is:
ID Name Roles Groups Profiles
1 admin Organization Administrator Public Contracts
System Administrator My Basket 1
My Basket 2
My Basket 3
4 docuware Organization Administrator Public Contracts
votosign My Basket 1
My Basket 2
My Basket 3
So basically for each user I need to remove any duplicate Roles, Groups, and Profiles regardless of what order they appear in the column (ie even if duplicates aren't "stacked" like they are in the Roles and Groups columns).
I'm pretty sure the answer is 'No', but here it goes:
Is there any way to output the desired result using Microsoft SQL?