Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
hendribt
Starting Member
4 Posts |
Posted - 2007-05-21 : 15:57:36
|
| I have a view that contains the following columns... AccountID, UserID, and RoleCode. (There are more columns but those are the important ones.) I need to select the Account ID where there is a record in the view that has both a role code of 1 and 3. The account ID shows up multiple times in this view, just with different role codes associated to it.Which would be a more efficient way of doing this...SELECT v1.AccountID, v1.UserID, OT.OrgName FROM View1 v1JOIN OrgTable OTON OT.AccountID = v1.AccountIDWHERE RoleCode = 1AND AccountID IN (SELECT AccountID FROM View1 WHERE RoleCode = 3)orSelect v1.AccountID, v1.UserID, OT.OrgName FROM View1 v1JOIN OrgTable OTON OT.AccountID = v1.AccountIDJOIN View1 vON v.AccountID = OT.AccountIDWHERE v.RoleCode = 3 and v1.RoleCode = 1or(anything better than the above two examples) |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-05-21 : 16:13:37
|
| neither.select accountIDfrom View1where roleCode in (1,3)group by accountIDhaving count(*) = 2If your view might return more than 1 row per AccountID/RoleCode combination, then change the last part to:having count(distinct RoleID) = 2- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
hendribt
Starting Member
4 Posts |
Posted - 2007-05-21 : 16:55:25
|
| Thank you for the reply! This isn't quite working because the query is a bit more complicated that I originally posted. I'll just post the whole query as I have it. I am currently selecting data for accounts that have a role code of 1. I need to get accounts that have an entry for both role codes of 1 and 2SELECT DA3.AccountID, DA3.PSUserId, DA3.RoleCode, DA3.RoleDesc, DA3.PrimaryFlag, DA3.EffectiveDate, DA3.ExpirationDate,U1.FirstName, U1.LastName,U1.JobTitle,U2.FirstName as ManagerFirstName,U2.LastName as ManagerLastName,U1.ManagerName,U1.SupervisorID,B2BC.Segment,B2BC.SegmentCD,B2BC.SubSegment,B2BC.SubSegmentCD,B2BC.Geo,B2BC.GeoCDFROM DA_vw3 DA3INNER JOIN Users U1 on DA3.PSUserId = U1.UserTokenINNER JOIN Users U2 on U1.SupervisorID = U2.UserTokenJOIN B2BC_vw3 B2BCON B2BC.AccountID = DA3.AccountIDWHERE DA3.RoleCode = 1 |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|
|
|
|
|