Solved

Remove duplicates from Left Outer Join

Posted on 2013-06-12
5
1,883 Views
Last Modified: 2013-06-12
Hi -
How do I remove duplicate Usernames from the query below.   I changed the query from a cross join to a left outer join to eliminate massive amounts of records.   Now I have the issue of retreiving multiple usernames (I'm grouping by MEMBEROF in the crystal report)

WITH EmployeeList AS
(SELECT DISTINCT   A.DistinguishedName,
                      A.AccountName,
                      A.DisplayName,
                      A.Type,
                      B.MEMBEROF,
                      B.USERNAME
FROM         ADUsersAndGroups A                
LEFT OUTER JOIN GROUPS B ON A.DistinguishedName = B.MEMBEROF
                 
UNION

SELECT DISTINCT  A.DistinguishedName,
                      A.AccountName,
                      A.DisplayName,
                      A.Type,
                      B.MEMBEROF,
                     B.USERNAME
FROM         ADUsersAndGroups A
LEFT OUTER JOIN GROUPS B ON A.AccountName = B.USERNAME)
SELECT * FROM EmployeeList
WHERE DisplayName is not NULL;


I'm trying to acheive the following output in the report:
Where user's are grouped by groupname.
MEMBEROF  (GROUPNAME)
          USERNAME (USERID)        DISPLAYNAME (User name)


However, I'm currently getting output were Display name is blank or populated with group information and the Username is diplaying multiple times.

Any help would be greatly appreciated.    Thanks!!  Mojeaux
0
Comment
Question by:mojeaux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 500 total points
ID: 39242734
I think you want to limit the first query to type = GROUP and the second to type = user

Can a group have a group?

mlmcc
0
 
LVL 4

Expert Comment

by:BAKADY
ID: 39242738
please post some example of the data/records you get duplicated...
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39242758
I think instead of 'UNION', you can use OR condition in left outer join:
SELECT DISTINCT   A.DistinguishedName, 
                      A.AccountName, 
                      A.DisplayName, 
                      A.Type,
                      B.MEMBEROF, 
                      B.USERNAME
FROM ADUsersAndGroups A                 
LEFT OUTER JOIN GROUPS B ON (A.DistinguishedName = B.MEMBEROF OR A.AccountName = B.USERNAME)

Open in new window

0
 
LVL 100

Expert Comment

by:mlmcc
ID: 39243324
The union is required since that makes it recursive.

mlmcc
0
 

Author Closing Comment

by:mojeaux
ID: 39243347
Thank you!  I was able to reach the same conclusion....thank goodness for you guys on EE!  Y'all are my 'goto' guys!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Summing a Function variable 5 44
VMware PVSCSI SQL Server 2016 AlwaysOn 2 37
Report 8 28
VB.Net CLR Assembly type Datatable into SQL 8 29
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

710 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