?
Solved

JOIN Question

Posted on 2007-11-19
3
Medium Priority
?
758 Views
Last Modified: 2010-04-21
I have 3 tables

aspnet_Membership (UserID)
aspnet_Roles(RoleID)
aspnet_UsersInRoles(RoleID,UserID)

Now I have pasted the code I am using at current but.  Becuase I have a user assigned to 2 roles that user appears in the reults.  I simply want to show unique users based on whether they are in any of the roles in the where clause.  If they are in 2 or more roles I still only wish them to appear in the results once.
SELECT        aspnet_Membership.ApplicationId, aspnet_Membership.UserId, aspnet_Membership.Password, aspnet_Membership.PasswordFormat, 
                         aspnet_Membership.PasswordSalt, aspnet_Membership.MobilePIN, aspnet_Membership.Email, aspnet_Membership.LoweredEmail, 
                         aspnet_Membership.PasswordQuestion, aspnet_Membership.PasswordAnswer, aspnet_Membership.IsApproved, aspnet_Membership.IsLockedOut, 
                         aspnet_Membership.CreateDate, aspnet_Membership.LastLoginDate, aspnet_Membership.LastPasswordChangedDate, 
                         aspnet_Membership.LastLockoutDate, aspnet_Membership.FailedPasswordAttemptCount, aspnet_Membership.FailedPasswordAttemptWindowStart, 
                         aspnet_Membership.FailedPasswordAnswerAttemptCount, aspnet_Membership.FailedPasswordAnswerAttemptWindowStart, 
                         aspnet_UsersInRoles.UserId AS Expr1, aspnet_Roles.RoleName
FROM            aspnet_Membership INNER JOIN
                         aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN
                         aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId LEFT OUTER JOIN
                         aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId
WHERE        (aspnet_Roles.RoleName = 'Administrator') OR
                         (aspnet_Roles.RoleName = 'POWERUSER')

Open in new window

0
Comment
Question by:REA_ANDREW
[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
3 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 20314501
You can't return the RoleName if you just want the user once.
SELECT        aspnet_Membership.ApplicationId, aspnet_Membership.UserId, aspnet_Membership.Password, aspnet_Membership.PasswordFormat, 
                         aspnet_Membership.PasswordSalt, aspnet_Membership.MobilePIN, aspnet_Membership.Email, aspnet_Membership.LoweredEmail, 
                         aspnet_Membership.PasswordQuestion, aspnet_Membership.PasswordAnswer, aspnet_Membership.IsApproved, aspnet_Membership.IsLockedOut, 
                         aspnet_Membership.CreateDate, aspnet_Membership.LastLoginDate, aspnet_Membership.LastPasswordChangedDate, 
                         aspnet_Membership.LastLockoutDate, aspnet_Membership.FailedPasswordAttemptCount, aspnet_Membership.FailedPasswordAttemptWindowStart, 
                         aspnet_Membership.FailedPasswordAnswerAttemptCount, aspnet_Membership.FailedPasswordAnswerAttemptWindowStart, 
--                         aspnet_UsersInRoles.UserId AS Expr1, aspnet_Roles.RoleName
FROM            aspnet_Membership 
WHERE  UserID IN ( 
                  Select Distinct UserID 
                  From aspnet_UsersInRoles Inner JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId
                  WHERE (aspnet_Roles.RoleName = 'Administrator') OR (aspnet_Roles.RoleName = 'POWERUSER')
                  );

Open in new window

0
 
LVL 42

Accepted Solution

by:
dqmq earned 2000 total points
ID: 20314614
>If they are in 2 or more roles I still only wish them to appear in the results once.

But then you can show at most one RoleName per userid in the output, right? I took the liberty to remove the outer join since at least one role (from the where clause) must be present.

SELECT 
 aspnet_Membership.ApplicationId,
 aspnet_Membership.UserId,
 aspnet_Membership.Password,
 aspnet_Membership.PasswordFormat, 
 aspnet_Membership.PasswordSalt,
 aspnet_Membership.MobilePIN,
 aspnet_Membership.Email,
 aspnet_Membership.LoweredEmail, 
 aspnet_Membership.PasswordQuestion,
 aspnet_Membership.PasswordAnswer,
 aspnet_Membership.IsApproved,
 aspnet_Membership.IsLockedOut, 
 aspnet_Membership.CreateDate,
 aspnet_Membership.LastLoginDate,
 aspnet_Membership.LastPasswordChangedDate,
 aspnet_Membership.LastLockoutDate,
 aspnet_Membership.FailedPasswordAttemptCount,
 aspnet_Membership.FailedPasswordAttemptWindowStart,
 aspnet_Membership.FailedPasswordAnswerAttemptCount,
 aspnet_Membership.FailedPasswordAnswerAttemptWindowStart,
 count(aspnet_Roles.RoleName) CountOfRoles
FROM aspnet_Membership
INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId
INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId
WHERE aspnet_Roles.RoleName in ('Administrator','POWERUSER')
GROUP BY
 aspnet_Membership.ApplicationId,
 aspnet_Membership.UserId,
 aspnet_Membership.Password,
 aspnet_Membership.PasswordFormat, 
 aspnet_Membership.PasswordSalt,
 aspnet_Membership.MobilePIN,
 aspnet_Membership.Email,
 aspnet_Membership.LoweredEmail, 
 aspnet_Membership.PasswordQuestion,
 aspnet_Membership.PasswordAnswer,
 aspnet_Membership.IsApproved,
 aspnet_Membership.IsLockedOut, 
 aspnet_Membership.CreateDate,
 aspnet_Membership.LastLoginDate,
 aspnet_Membership.LastPasswordChangedDate,
 aspnet_Membership.LastLockoutDate,
 aspnet_Membership.FailedPasswordAttemptCount,
 aspnet_Membership.FailedPasswordAttemptWindowStart,
 aspnet_Membership.FailedPasswordAnswerAttemptCount,
 aspnet_Membership.FailedPasswordAnswerAttemptWindowStart

Open in new window

0
 
LVL 20

Author Closing Comment

by:REA_ANDREW
ID: 31409996
I have tried both and your solution worked.  Thankyou
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

762 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