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 |
|
boybles
Starting Member
23 Posts |
Posted - 2007-07-26 : 09:21:10
|
I'm stuck trying to pull together what is for me a complex join statement. I have 8 tables following, and would like to figure out how to extract all users and roles (if they have any associated) when given a company and division. I have given a couple examples the way they should be output.RoleTbl----------------RoleID|RoleName----------------1|Admin2|User3|GuestRoleDivisionTbl---------------RoleID|DivisionID-----------------1|12|13|12|2DivisionTbl----------------DivisionID|DivisionName----------------1|Accounting2|Marketing3|SalesCompanyDivisionTbl----------------CompanyID|DivisionID----------------1|11|21|32|2CompanyTbl----------------CompanyID|CompanyName----------------1|Gap2|MayCo3|SearsUserRoleTbl----------------UserID|RoleID----------------1|12|33|26|2UserTbl----------------UserID|UserName1|Jim2|Frank3|Kelly4|Al5|James6|Tom7|GeorgeCompanyUserTbl----------------CompanyID|UserID----------------1|11|21|31|41|52|62|7Ex1:If Company=1 and Division=1Output:UserID|UserName|RoleID|RoleName1|Jim|1|Admin2|Frank|3|Guest3|Kelly|2|User4|Al|Null|Null5|James|Null|NullEx2:If Company=2 and Division=2Output:UserID|UserName|RoleID|RoleName6|Tom|2|User7|George|Null|NullPlease help!!! Thanks!!!Boybles |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-26 : 11:37:21
|
[code]-- Prepare sample dataDECLARE @RoleTbl TABLE (RoleID INT, RoleName SYSNAME)INSERT @RoleTblSELECT 1, 'Admin' UNION ALLSELECT 2, 'User' UNION ALLSELECT 3, 'Guest'DECLARE @RoleDivisionTbl TABLE (RoleID INT, DivisionID INT)INSERT @RoleDivisionTblSELECT 1, 1 UNION ALLSELECT 2, 1 UNION ALLSELECT 3, 1 UNION ALLSELECT 2, 2DECLARE @DivisionTbl TABLE (DivisionID INT, DivisionName SYSNAME)INSERT @DivisionTblSELECT 1, 'Accounting' UNION ALLSELECT 2, 'Marketing' UNION ALLSELECT 3, 'Sales'DECLARE @CompanyDivisionTbl TABLE (CompanyID INT, DivisionID INT)INSERT @CompanyDivisionTblSELECT 1, 1 UNION ALLSELECT 1, 2 UNION ALLSELECT 1, 3 UNION ALLSELECT 2, 2DECLARE @CompanyTbl TABLE (CompanyID INT, CompanyName SYSNAME)INSERT @CompanyTblSELECT 1, 'Gap' UNION ALLSELECT 2, 'MayCo' UNION ALLSELECT 3, 'Sears'DECLARE @UserRoleTbl TABLE (UserID INT, RoleID INT)INSERT @UserRoleTblSELECT 1, 1 UNION ALLSELECT 2, 3 UNION ALLSELECT 3, 2 UNION ALLSELECT 6, 2DECLARE @UserTbl TABLE (UserID INT, UserName SYSNAME)INSERT @UserTblSELECT 1, 'Jim' UNION ALLSELECT 2, 'Frank' UNION ALLSELECT 3, 'Kelly' UNION ALLSELECT 4, 'Al' UNION ALLSELECT 5, 'James' UNION ALLSELECT 6, 'Tom' UNION ALLSELECT 7, 'George'DECLARE @CompanyUserTbl TABLE (CompanyID INT, UserID INT)INSERT @CompanyUserTblSELECT 1, 1 UNION ALLSELECT 1, 2 UNION ALLSELECT 1, 3 UNION ALLSELECT 1, 4 UNION ALLSELECT 1, 5 UNION ALLSELECT 2, 6 UNION ALLSELECT 2, 7-- Show the expected outputSELECT cu.UserID, u.UserName, ur.RoleID, r.RoleNameFROM @CompanyDivisionTbl AS cdINNER JOIN @CompanyUserTbl AS cu ON cu.CompanyID = cd.CompanyIDINNER JOIN @UserTbl AS u ON u.UserID = cu.UserIDLEFT JOIN @UserRoleTbl AS ur ON ur.UserID = u.UserIDLEFT JOIN @RoleTbl AS r ON r.RoleID = ur.RoleIDWHERE cd.CompanyID = 1 AND cd.DivisionID = 1[/code] E 12°55'05.76"N 56°04'39.42" |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-26 : 11:43:02
|
Sprock...Please rephrase question. Bot does not understand Just kidding:Today I have put in glass window in toliet door, cut doors to fit new floor, changed brake plates and brake disc on our second car, picked berries and made lemonade.And painted living room with two coates of paint. And installed our new oven and glass induction stove.And answered some posts here at SQLTeam. E 12°55'05.76"N 56°04'39.42" |
 |
|
|
boybles
Starting Member
23 Posts |
Posted - 2007-07-26 : 14:09:22
|
Peso,Thank you so much for your help on this. I'm really trying to grasp the concepts of joins and am a very visual/example oriented person, so your time and help is immensely appreciated. I've tried your suggestion, and it checks out for the examples I posted. But when checking against the RoleDivisionTbl, something is not working. In the example, there are all 3 roles for division 1 but only 1 role assigned to division 2 (which is only active in MayCo). Yet, I get the same assigned roles listed whether I put division 1,2 or 3 for company 1 (Gap). How do I factor in RoleDivisionTbl so that only assigned roles for a given division show up, otherwise show a Null assignment for every user in the company.ieIf Company=1 and (Division=1 or 2 or 3)Current Output:UserID|UserName|RoleID|RoleName1|Jim|1|Admin2|Frank|3|Guest3|Kelly|2|User4|Al|Null|Null5|James|Null|NullIf Company=1 and (Division=2 or Division=3)Desired Output:UserID|UserName|RoleID|RoleName1|Jim|Null|Null2|Frank|Null|Null3|Kelly|Null|Null4|Al|Null|Null5|James|Null|NullThanks again!!!boybles |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-26 : 19:29:31
|
Please post some PK FK information too. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-26 : 19:40:16
|
[code]SELECT u.UserID, u.UserName, d.RoleID, d.RoleNameFROM @CompanyUserTbl AS cuINNER JOIN @UserTbl AS u ON u.UserID = cu.UserIDLEFT JOIN ( SELECT DISTINCT ur.UserID, r.RoleID, r.RoleName FROM @CompanyDivisionTbl as cd INNER JOIN @RoleDivisionTbl AS rd ON rd.DivisionID = cd.DivisionID INNER JOIN @UserRoleTbl AS ur ON ur.RoleID = rd.RoleID INNER JOIN @RoleTbl AS r ON r.RoleID = ur.RoleID WHERE cd.CompanyID = 1 AND cd.DivisionID IN (2, 3) ) AS d ON d.UserID = u.UserIDWHERE cu.CompanyID = 1[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
boybles
Starting Member
23 Posts |
Posted - 2007-07-26 : 20:20:27
|
Peso,That is brilliant. There's no way I could come up with that on my own at my level. As somebody who is just starting to learn joins, what advice do you have in putting together all these pieces? Do you have a systematic way of examining? Again...thank you for your help here. You have helped me in developing my software more than you know.boybles |
 |
|
|
|
|
|
|
|