Inner join une table de tableau croisé dynamic SQL

Je ne peux pas sembler joindre intérieur à une table d'enseignants afin d'get le premier et le nom de famille.

select * from BookingDays bd inner join Teachers t on t.ID = bd.TeacherID pivot ( max (bd.BookingDuration) for bd.DayText in ([MONDAY], [TUESDAY], [WEDNESDAY], [THURSDAY], [FRIDAY]) ) as MaxBookingDays where bd.BookingDate >= (SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0)) and bd.BookingDate <= (SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 6)) ' 

Le message d'erreur que je reçois est –

 Msg 8156, Level 16, State 1, Line 3 The column 'ID' was specified multiple times for 'MaxBookingDays'. Msg 4104, Level 16, State 1, Line 4 The multi-part identifier "bd.BookingDate" could not be bound. Msg 4104, Level 16, State 1, Line 4 The multi-part identifier "bd.BookingDate" could not be bound. 

L'erreur ci-dessous est due au fait que vous ne spécifiez pas vos colonnes:

Msg 8156, niveau 16, état 1, ligne 3

La colonne 'ID' a été spécifiée plusieurs fois pour 'MaxBookingDays'.

Donc, je voudrais changer légèrement votre requête à quelque chose comme ceci:

 select * from ( -- don't use select *, call out your fields -- and use a sub-query with your WHERE inside the subquery select bd.BookingDuration, bd.Otherfields, t.Fields from BookingDays bd inner join Teachers t on t.ID = bd.TeacherID where bd.BookingDate >= (SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0)) and bd.BookingDate <= (SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 6)) ) src pivot ( max (BookingDuration) for DayText in ([MONDAY], [TUESDAY], [WEDNESDAY],[THURSDAY], [FRIDAY]) ) as MaxBookingDays