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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Help

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 no

Thanks 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_id
left join titles t on t.titleid = dt.titleid

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 01:02:12
[code]

DECLARE @DoctorID int
SET @DoctorID= <set your required doctor id here>

select m.Name,m.Title,
CASE WHEN dt.DoctorID IS NULL THEN 'False' ELSE 'True' END AS Assigned
from
(
select d.Name,t.TitleID,t.Title
from Doctor d
cross join Titles t
where d.DoctorID=@DoctorID
)m
left join Doctor_Titles dt
on dt.DoctorID = m.DoctorID
AND dt.TitleID = m.TitleID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 02:57:03
oops i missed that column in derived table

DECLARE @DoctorID int
SET @DoctorID= <set your required doctor id here>

select m.Name,m.Title,
CASE WHEN dt.DoctorID IS NULL THEN 'False' ELSE 'True' END AS Assigned
from
(
select d.DoctorID,d.Name,t.TitleID,t.Title
from Doctor d
cross join Titles t
where d.DoctorID=@DoctorID
)m
left join Doctor_Titles dt
on dt.DoctorID = m.DoctorID
AND dt.TitleID = m.TitleID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mike1973
Starting Member

46 Posts

Posted - 2011-10-11 : 03:01:54
Thanks so much, perfect.

I appreciate your help.

Mike
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-11 : 03:51:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -