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 |
|
thiruna
Starting Member
41 Posts |
Posted - 2003-05-31 : 01:29:40
|
| I have the following tables...1. Divisiondiv_no-----------div_name1----------------div012----------------div022.companycomp_no----------comp_name1----------------comp12----------------comp23.usersuser_no----------comp_no-----------div_no-----user_admin1----------------1-----------------1----------12----------------1-----------------1----------23----------------1-----------------2----------14----------------1-----------------2----------15----------------2-----------------1----------16----------------2-----------------1----------17----------------2-----------------2----------18----------------2-----------------2----------14.projectsproj_no----------proj_name---------div_no1----------------prj01-------------12----------------prj02-------------13----------------prj03-------------24----------------prj04-------------25----------------prj05-------------25.company_projectscomp_no----------proj_no-----------include(yes-1/No-0)1----------------1-----------------11----------------2-----------------11----------------3-----------------11----------------4-----------------01----------------5-----------------12----------------1-----------------02----------------2-----------------02----------------3-----------------02----------------4-----------------12----------------4-----------------1Now i want to list the projects for user_no=1(i mean it should select the comp_no and div_no from the users table and then select the projects for the company where include =1 from company_project table and then filter the results for the users division.)Here for this query("list projects for user_no=1), it should return the following rows.user_no------comp_no--------div_no----------proj_no---div_no1------------1--------------1---------------1---------11------------1--------------1---------------2---------1and for this query("list projects for user_no=2 who has user_admin=2), it should return the following rows.user_no------comp_no--------div_no----------proj_no-----div_no1------------1--------------1---------------1-----------11------------1--------------1---------------2-----------11------------1--------------1---------------3-----------21------------1--------------1---------------5-----------2I have tried with the following select command...SELECT usr.user_no, usr.comp_no, usr.div_no, prj.proj_no, prj.div_noFROM users usrINNER JOIN projects prj ON usr.div_no =prj.div_noINNER JOIN company_projects comp ON prj.proj_no =comp.proj_noWHERE prj.div_no=isnull(SELECT case when user_admin=2 Then null Else division_no end FROM users WHERE user_no =1), prj.div_no))AND comp.include=1But above statement doesnt return the required results... Can you please help me to frame a correct statement?Many thanks in advanceThiruthiru.nachi@cloughindia.com |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-05-31 : 03:13:02
|
| SELECT usr.user_no, usr.comp_no, usr.div_no, prj.proj_no, prj.div_noFROM users usrINNER JOIN company_projects comp ON usr.comp_no =comp.comp_noINNER JOIN projects prj ON prj.proj_no=comp.proj_no AND prj.div_no = usr.div_noWHERE usr.user_no = 1 AND comp.include = 1You don't describe what user_admin is about, but your WHERE suggests that you don't want rows with user_admin = 2? Just add AND usr.user_admin <> 2 to the WHERE.Sam |
 |
|
|
thiruna
Starting Member
41 Posts |
Posted - 2003-05-31 : 03:45:25
|
| User_admin is the administrator level and here it is as follows...Normal user =1Administrator =2So when listing projects, it should list all projects from all divisions for that company if user_admin is 2. (Note: it should pick up the user_admin value by itself using user_no)any suggesstions please.thanksthiruEdited by - thiruna on 05/31/2003 03:46:32 |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-05-31 : 09:21:37
|
| SELECT usr.user_no, usr.comp_no, usr.div_no, prj.proj_no, prj.div_noFROM users usrINNER JOIN company_projects comp ON usr.comp_no = comp.comp_noINNER JOIN projects prj ON prj.proj_no = comp_proj_noWHERE prj.div_no=isnull(SELECT case when user_admin=2 Then null Else division_no end FROM users WHERE user_no =1), prj.div_no))AND comp.include=1 AND usr.user_no = 1Sam |
 |
|
|
thiruna
Starting Member
41 Posts |
Posted - 2003-06-01 : 04:46:25
|
| Thanks Sam,Is there any good link to learn abour Inner join in details...Thanks agn. for your support...Thiru |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2003-06-01 : 07:49:49
|
| Books Online is probably your best bet.Or one of the books in the SQLTeam Store. "Professional SQL Server 2000 Programming" is probably a good choice.DamianEdited by - merkin on 06/01/2003 07:50:13 |
 |
|
|
|
|
|
|
|