生成PIVOT表

时间:2013-06-25 09:08:35

标签: sql sql-server sql-server-2008 pivot pivot-table

我正在尝试找到在报告(rdlc)中生成下表的最佳方法 -

Desired pivot table required

这显示了最左侧列中的教师姓名(从教师表中拉出),然后是星期一到星期五的预订(从预订日表中提取)。在行中,AM预订(BookingDuration:0)将位于第一行,PM预订(BookingDuration:1)将位于底行,最后一整天(BookingDuration:2)将位于顶行但是大写(在RDLC中处理)。

我遇到的问题是从SQL生成一个快速而整洁的表。我相信我需要制作一个PIVOT表,但是我尝试了很多不同的方法。

还需要考虑每小时预订(BookingDuration:3)。每个预订日都有开始和结束时间。所以如果开始时间是< 12:00和结束时间> 12:00,全天。如果开始和结束时间< 12:00然后AM,最后如果结束时间和开始时间> 12:00,全天。可能还有2x AM每小时预订(09:00-10:00和10:30 - 11:30),但从不超过2.这同样适用于PM。发生这种情况时,第一个AM将位于顶部框中,第二个AM将位于底部框中。我得到保证,2x AM和1x PM预订将永远不会发生,因此我在创建预订表单中添加了验证以防止这种情况发生。

每位老师都有一排。如果教师没有预订,教师仍会显示在报告上,但值将为空。

我的表格(BookingDays)如下 -

ID - INT(PK)
BookingID - INT
BookingDate - date
DayText - VARCHAR(50)
StartTIme - decimal
EndTime - decimal
TeacherID int
BookingDuration - int
NoOfHours - decimal
TotalChargeAmount - decimal
TotalPayAmount - decimal
Status - int

还有一个包含全名等的教师表。这个表由TeacherID列加入到BookingDays表中。

有没有人知道我该怎么做呢?

这是我到目前为止所尝试的(由于文本限制仅显示星期五)。这个的问题是,如果有2 -

,我无法获得每小时的AM / PM
with CTE_D as 
        ( 
            SELECT DATEADD(ww, DATEDIFF(ww,0,CONVERT(date, @WeekStart, 103)), 0) as BookingDate 

            union all 

            select DATEADD(day, 1, BookingDate) 
            from CTE_D where BookingDate < DATEADD(ww, DATEDIFF(ww,0,CONVERT(date, @WeekStart, 103)), 6)
        )
        SELECT 
            t.Firstname, 
            t.Surname, t.Telephone, t.Mobile, t.Band, tb.Band, t.DefaultChargeRateDaily as 'TeacherDefaultChargeRateDaily', t.DefaultPayRateDaily as 'TeacherDefaultPayRateDaily', t.DefaultChargeRateAM, t.DefaultChargeRatePM, t.DefaultChargeRateDaily, t.DefaultPayRateAM, t.DefaultPayRatePM, t.DefaultPayRateDaily,
            --------------------------------------------------------------------------------------------------------------------------
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayAM",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayAMTotalChargeAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayAMTotalPayAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 0 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayAMBandBookedAt",

            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayPM", 
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayPMTotalChargeAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayPMTotalPayAmount",           
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 1 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayPMBandBookedAt",

            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayDaily",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayDailyTotalChargeAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayDailyTotalPayAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingType = 0 and bd.BookingDuration = 2 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayDailyBandBookedAt",

            COUNT(DISTINCT CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.ID END) as "FridayHourlyAMCount",    
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyAM",                 
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyAMSchoolName",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyAMTimes",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyAMTotalChargeAmount",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyAMTotalPayAmount",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyAMBandBookedAt",

            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyAM2",                    
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyAM2SchoolName",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyAM2Times",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyAM2TotalChargeAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyAM2TotalPayAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) < CONVERT(time(0), '12:00:00') AND bd.NoOfHOurs < 5.5 and s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyAM2BandBookedAt",

            COUNT(DISTINCT CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 and s.PrimarySchool = 1 THEN bd.ID END) as "FridayHourlyPMCount",   
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyPM",                            
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyPMSchoolName",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyPMTimes",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyPMTotalChargeAmount",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyPMTotalPayAmount",
            MAX(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyPMBandBookedAt",

            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "FridayHourlyPM2",                           
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN s.SchoolName ELSE NULL END) "FridayHourlyPM2SchoolName",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyPM2Times",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyPM2TotalChargeAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyPM2TotalPayAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and CONVERT(time(0), bd.StartTime) >= CONVERT(time(0), '12:00:00') and bd.NoOfHours < 5.5 AND s.PrimarySchool = 1 THEN bd.BandBookedAt ELSE NULL END) "FridayHourlyPM2BandBookedAt",

            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.ID ELSE NULL END) "FridayHourlyFullDay",             
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN s.SchoolName ELSE NULL END) "FridayHourlyFullDaySchoolName",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN REPLACE(REPLACE(LTRIM(RIGHT(CONVERT(VARCHAR, bd.StartTime, 100), 7)) + '-' + LTRIM(RIGHT(CONVERT(VARCHAR(20), bd.EndTime, 100), 7)), 'AM',''), 'PM', '') ELSE NULL END) "FridayHourlyFullDayTimes",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.TotalChargeAmount ELSE NULL END) "FridayHourlyFullDayTotalChargeAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.TotalPayAmount ELSE NULL END) "FridayHourlyFullDayTotalPayAmount",
            MIN(CASE WHEN bd.DayText = 'Friday' and bd.BookingDuration = 3 and bd.NoOfHours >= 5.5 and s.PrimarySchool = 1 AND CONVERT(time(0), bd.EndTime) > CONVERT(time(0), '12:00:00') THEN bd.BandBookedAt ELSE NULL END) "FridayFullDayBandBookedAt",
            COUNT(CASE WHEN s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "BookingCount",
            --------------------------------------------------------------------------------------------------------------------------
            t.Notes, t.DefaultChargeRateDaily, t.DefaultPayRateDaily 
                FROM Teachers t 
                cross join CTE_D d 
                inner join TeacherBands tb 
                    on t.Band = tb.ID
                left join BookingDays bd 
                    on t.ID = bd.TeacherID and 
                        bd.BookingDate = d.BookingDate and bd.BookingType = 0
                left join BookingDurations bds 
                    on bd.BookingDuration = bds.ID 
                left join BookingTypes bt
                    on bd.BookingType = bt.ID
                left join Bookings b 
                    on bd.BookingID = b.ID
                left join Schools s 
                    on b.School = s.ID and s.PrimarySchool = 1
                    WHERE Active = 0 and (bd.Status = 0 or bd.Status IS NULL) and (t.Status != 2) and t.PrimarySchool = 1
                GROUP BY Firstname, Surname, t.Telephone, t.Mobile, t.Notes, t.Band, tb.Band, t.DefaultPayRateDaily, t.DefaultChargeRateDaily, t.DefaultChargeRateAM, t.DefaultChargeRatePM, t.DefaultChargeRateDaily, t.DefaultPayRateAM, t.DefaultPayRatePM, t.DefaultPayRateDaily
                ORDER BY Surname, Firstname ASC

希望这是有道理的。我试图调整我之前使用的数据透视表,但无济于事。当它输出到最后一列时,我似乎无法显示学校名称。

    WITH Bookings AS
(  SELECT  TeacherID,
            [WeekDay] = DATENAME(WEEKDAY, BookingDate),
            [0], [1], [2], [3],
            [Status] = CASE 
                            WHEN ([0] > 0 AND [1] > 0) THEN 'XXX'
                            WHEN [2] > 0 THEN 'XXX'  
                            WHEN [0] > 0 THEN 'PM'
                            WHEN [1] > 0 THEN 'AM'  
                            WHEN [3] > 0 AND StartTime <= CONVERT(TIME, '12:00:00') AND EndTime >= CONVERT(TIME, '12:00:00') THEN 'XXX'   
                            WHEN [3] > 0 AND EndTime <= CONVERT(TIME, '12:00:00') THEN 'PM'
                            WHEN [3] > 0 AND StartTime >= CONVERT(TIME, '12:00:00') THEN 'AM'                     

                        END
    FROM    (   SELECT  TeacherID, 
                        BookingDate, 
                        BookingDuration, 
                        StartTime = CASE WHEN BookingDuration = 3 THEN CAST(MIN(StartTime) OVER(PARTITION BY TeacherID, BookingDate, BookingDuration) AS TIME) ELSE NULL END,
                        EndTime = CASE WHEN BookingDuration = 3 THEN CAST(MAX(EndTime) OVER(PARTITION BY TeacherID, BookingDate, BookingDuration) AS TIME) ELSE NULL END,                        
                        [x] = 1
                FROM    BookingDays bd
                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,'06/17/2013'), 0) AND BookingDate <= DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 6)

), PivotedBookings AS
(   SELECT  *
    FROM    Bookings
            PIVOT
            (   MAX([Status])
                FOR [WeekDay] IN ([MondayAM1], [MondayAM2], [MondayPM1], [MondayPM2], [MondayFullDay])
            ) pvt

)
SELECT ID,Firstname,Surname,Band,'£' + CONVERT(varchar(50),DefaultChargeRateDaily) + '/' + '£' + CONVERT(varchar(50), DefaultPayRateDaily) as 'BandRates',Telephone,Mobile,Teacher,TeacherAssistant,KeyStage,MAX(MondayAM1) MondayAM1,MAX(MondayAM2) MondayAM2,MAX(MondayPM1) MondayPM1,MAX(MondayPM2) MondayPM2,MAX(MondayFullDay) MondayFullDay, Notes
  FROM (
SELECT  t.ID, 
        t.Firstname, 
        t.Surname, 
        tb.Band,
        t.DefaultChargeRateDaily,
        t.DefaultPayRateDaily,
        t.Telephone,
        t.Mobile,
        t.Teacher,
        t.TeacherAssistant,
        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 'Y8' WHEN t.Year7 > 0 THEN 'Y7' WHEN t.Year6 > 0 THEN 'Y6' WHEN t.Year5 > 0 THEN 'Y5' 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',

        MondayAM1 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX'  ELSE COALESCE(pb.MondayAM1, '') END,
        MondayAM2 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX'  ELSE COALESCE(pb.MondayAM2, '') END,
        MondayPM1 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX'  ELSE COALESCE(pb.MondayPM1, '') END,
        MondayPM2 = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX'  ELSE COALESCE(pb.MondayPM2, '') END,
        MondayFullDay = CASE WHEN an.Date = DATEADD(ww, DATEDIFF(ww,0,'06/17/2013'), 0) AND an.TeacherID = t.ID THEN an.Text WHEN t.Status = 0 THEN 'XXX'  ELSE COALESCE(pb.MondayFullDay, '') END,
        Notes
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
            ) T1
 GROUP BY ID,Firstname,Surname,Telephone,Mobile,Teacher,TeacherAssistant,KeyStage,Notes,DefaultChargeRateDaily,DefaultPayRateDaily,Band
 ORDER BY Surname,Firstname asc

0 个答案:

没有答案