Solved

Remove duplicates from Left Outer Join

Posted on 2013-06-12
5
1,793 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

708 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

13 Experts available now in Live!

Get 1:1 Help Now