0
SQL MIN和MAX的關係,我從一個SQL表,可以使用下面的代碼檢索列 -在SELECT CASE
MAX(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM",
MAX(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAMSchoolName",
MAX(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAMTimes",
MAX(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAMTotalChargeAmount",
MAX(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAMTotalPayAmount",
MAX(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAMBandBookedAt",
MIN(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM2",
MIN(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM2SchoolName",
MIN(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM2Times",
MIN(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM2TotalChargeAmount",
MIN(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM2TotalPayAmount",
MIN(CASE WHEN bd.DayText = 'Monday' 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) "MondayHourlyAM2BandBookedAt",
運行此代碼時,我得到了2個預訂,但返回的充電/收費率爲其他預訂。即 -
應該是 預訂1-電荷£20,£付費10 預訂2-充電£100,付費£50
但即時得到 預訂1 - 充電£100,付費£50 預訂2 - 收費£20,支付£10
我以爲這是因爲它使用MAX和MIN返回最高和最低值。我如何將MAX和MIN值返回到「HourlyAM」列?
希望我解釋得很清楚!由於
給出幾乎所有的case語句具有相同的邏輯,您應該將common子句從case中移出並放入查詢的主'where'子句中。 –
這是一個更大的查詢的一小部分。 (IE:AM,PM,FullDay,AMHourly,PMHourly,FullDayHourly)每週的每一天都有這些段落中的6段,類似但持續時間不同。我意識到它不是最有效的方式,但它似乎運作良好,而且速度很快。因此,我認爲我不能將共同的條款移到主要的where子句中。乾杯 – dynamicuser