以下の SQL ステートメントはピボット テーブルを生成しますが、これらはすべて正しく機能しています。ただし、コードの最後で、別のテーブル (availabilitynotes) の値を表示しようとしています。この例では、以下のメモは「A/P」になります。各日は、個々の日に 0 または 1 つのメモを持ちます。メモがある場合は、メモが表示されます。ノートがない場合は、COALESCE 値が表示されます。ただし、1 人の教師のメモが月曜日に設定され、1 人の教師が水曜日に設定されている場合は、重複した行が返されます。1 つの行ですべての値を返すように設定するにはどうすればよいですか?
WITH Bookings AS
( SELECT TeacherID,
[WeekDay] = DATENAME(WEEKDAY, BookingDate),
[Status] = CASE WHEN [2] > 0 THEN 'XXX'
WHEN [0] > 0 AND [1] > 0 THEN 'XXX'
WHEN [0] > 0 THEN 'PM'
WHEN [1] > 0 THEN 'AM'
WHEN [3] > 0 AND CONVERT(time(0), EndTime) <= CONVERT(time(0), '12:00:00') and CONVERT(time(0), StartTime) < CONVERT(time(0), '12:00:00') THEN 'PM'
WHEN [3] > 0 AND CONVERT(time(0), StartTime) >= CONVERT(time(0), '12:00:00') THEN 'AM'
WHEN [3] > 0 AND CONVERT(time(0), StartTime) <= CONVERT(time(0), '12:00:00') AND CONVERT(time(0), EndTime) >= CONVERT(time(0), '12:00:00') THEN 'XXX'
END
FROM ( SELECT TeacherID, BookingDate, BookingDuration, StartTime, EndTime, [X] = 1
FROM BookingDays where (Status = 0 or Status IS NULL)
) BookingDays
PIVOT
( SUM(X)
FOR BookingDuration IN ([0], [1], [2], [3])
) pvt
WHERE BookingDate >= DATEADD(ww, DATEDIFF(ww,0,@Date), 0) AND BookingDate <= DATEADD(ww, DATEDIFF(ww,0,@Date), 6)
), PivotedBookings AS
( SELECT *
FROM Bookings
PIVOT
( MAX([Status])
FOR [WeekDay] IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday])
) pvt
)
SELECT t.ID,
t.Firstname,
t.Surname,
CASE WHEN t.Nursery > 0 THEN 'NUR' WHEN t.Reception > 0 THEN 'REC' WHEN t.Year1 > 0 THEN 'Y1' WHEN t.Year2 > 0 THEN 'Y2' WHEN t.Year3 > 0 THEN 'Y3' WHEN t.Year4 > 0 THEN 'Y4' WHEN t.Year5 > 0 THEN 'Y5' WHEN t.Year6 > 0 THEN 'Y6' WHEN t.Year7 > 0 THEN 'Y7' WHEN t.Year8 > 0 THEN 'Y8' WHEN t.Year9 > 0 THEN 'Y9' WHEN t.Year10 > 0 THEN 'Y10' WHEN t.Year11 > 0 THEN 'Y11' WHEN t.ALevel > 0 THEN 'ALevel' END + ' - ' + CASE WHEN t.ALevel > 0 THEN 'ALevel' WHEN t.Year11 > 0 THEN 'Y11' WHEN t.Year10 > 0 THEN 'Y10' WHEN t.Year9 > 0 THEN 'Y9' WHEN t.Year8 > 0 THEN 'Y7' WHEN t.Year6 > 0 THEN 'Y6' WHEN t.Year5 > 0 THEN 'Y6' WHEN t.Year4 > 0 THEN 'Y4' WHEN t.Year3 > 0 THEN 'Y3' WHEN t.Year2 > 0 THEN 'Y2' WHEN t.Year1 > 0 THEN 'Y1' WHEN t.Reception > 0 THEN 'REC' WHEN t.Nursery > 0 THEN 'NUR' ELSE '' END as 'KeyStage',
Monday = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,@Date), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.Monday, '') END,
Tuesday = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,@Date), 1) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.Tuesday, '') END,
Wednesday = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,@Date), 2) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.Wednesday, '') END,
Thursday = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,@Date), 3) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.Thursday, '') END,
Friday = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,@Date), 4) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX' ELSE COALESCE(pb.Friday, '') END
FROM Teachers t
LEFT JOIN PivotedBookings pb
ON pb.TeacherID = t.ID
LEFT JOIN TeacherBands tb
ON tb.ID = t.Band
LEFT JOIN AvailabilityNotes an
ON t.ID = an.TeacherID
WHERE t.Active = 0 and (t.Status = 1 or t.Status = 0) and t.PrimarySchool = 1
ORDER BY t.Surname, t.Firstname asc
これにより、次の出力が生成されます -
ID | Firstname | Surname | Mon | Tue | Wed | Thu | Fri
1 Steve Smith XXX PM AM A/P NULL
1 Steve Smith XXX PM AM NULL A/P
必要なときに -
ID | Firstname | Surname | Mon | Tue | Wed | Thu | Fri
1 Steve Smith XXX PM AM A/P A/P
ありがとう