select
id_TimeSlot
, coalesce(Thera_A, 'available') as Thera_A
, coalesce(Thera_B, 'available') as Thera_B
, coalesce(Thera_C, 'available') as Thera_C
from
(
select
t.id_TimeSlot
, max(case b.id_Therapist when 1 then 'booked' else null end) as Thera_A
, max(case b.id_Therapist when 2 then 'booked' else null end) as Thera_B
, max(case b.id_Therapist when 3 then 'booked' else null end) as Thera_C
from TimeSlot as t
left join BookSlot as b on b.id_TimeSlot = t.id_TimeSlot
left join Therapist as p on p.id_Therapist = b.id_Therapist
group by t.id_TimeSlot
) as xx ;
:
create table TimeSLot (id_TimeSLot integer);
create table Therapist (id_Therapist integer);
create table BookSlot (id_Therapist integer, id_TimeSlot integer);
insert into Therapist (id_Therapist)
values (1), (2), (3);
insert into TimeSlot (id_TimeSlot)
values (1), (2), (3), (4), (5);
insert into BookSlot (id_Therapist,id_TimeSlot)
values (1,1), (1,5), (2,1), (2,4), (3,1);
id_TimeSlot Thera_A Thera_B Thera_C
----------------------------------------------
1 booked booked booked
2 available available available
3 available available available
4 available booked available
5 booked available available