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 |
|
mike1973
Starting Member
46 Posts |
Posted - 2011-10-10 : 17:55:48
|
| Hello guys,I have a table called Doctor(DoctorID,Name), Titles (TitleID,Title) and a Doctor_Titles(DoctorID,TitleID)I'm not sure how to get the following result:I need for a selected doctor all titles in the titles table and another column (True or false) if assigned to the selected doctor or noThanks a lot for your help |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-10-10 : 18:38:59
|
| select d.DoctorID,d.Name ,isnull(t.title,'Not Assigned')from doctor d inner join doctor_titles dt on d.doctorid = dt.doctor_idleft join titles t on t.titleid = dt.titleidJimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-11 : 01:02:12
|
| [code]DECLARE @DoctorID intSET @DoctorID= <set your required doctor id here>select m.Name,m.Title,CASE WHEN dt.DoctorID IS NULL THEN 'False' ELSE 'True' END AS Assignedfrom(select d.Name,t.TitleID,t.Titlefrom Doctor dcross join Titles twhere d.DoctorID=@DoctorID)mleft join Doctor_Titles dton dt.DoctorID = m.DoctorIDAND dt.TitleID = m.TitleID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-10-11 : 02:34:11
|
| Dear Visakh,Thanks for your help, when i tried to execute the query i receive an error at this line"on dt.DoctorID = m.DoctorID"not sure why even it looks good, Invalid column name 'DoctorID'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-11 : 02:57:03
|
oops i missed that column in derived tableDECLARE @DoctorID intSET @DoctorID= <set your required doctor id here>select m.Name,m.Title,CASE WHEN dt.DoctorID IS NULL THEN 'False' ELSE 'True' END AS Assignedfrom(select d.DoctorID,d.Name,t.TitleID,t.Titlefrom Doctor dcross join Titles twhere d.DoctorID=@DoctorID)mleft join Doctor_Titles dton dt.DoctorID = m.DoctorIDAND dt.TitleID = m.TitleID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mike1973
Starting Member
46 Posts |
Posted - 2011-10-11 : 03:01:54
|
Thanks so much, perfect.I appreciate your help. Mike |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-11 : 03:51:51
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|