我正在使用以下代码从SQL表中检索列 -
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返回最高和最低的im。如何将返回到“HourlyAM”列的MAX和MIN值关联起来?
希望我解释得很好!感谢
答案 0 :(得分:2)
如果不更好地了解您的数据,我不会尝试涉及您的标准,但我认为您最好使用ROW_NUMBER():
SELECT *
FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY MondayHourlyAMTimes DESC) 'Booking1'
, ROW_NUMBER() OVER (ORDER BY MondayHourlyAMTimes ) 'Booking2'
FROM Table
WHERE common criteria
)
WHERE Booking1 = 1 OR Booking2 = 1
这里的想法是,您根据一个字段为符合条件的每一行分配排名,然后根据该排名返回完整行。