Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove duplicates from Left Outer Join

Posted on 2013-06-12
5
Medium Priority
?
2,043 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 101

Accepted Solution

by:
mlmcc earned 2000 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 101

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

715 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