0

我正在使用以下代码从 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 个预订,但返回的收费/支付费率适用于另一个预订。IE -

应该是预订 1- 收费 20 英镑,支付 10 英镑 预订 2- 收费 100 英镑,支付 50 英镑

但我收到预订 1 - 收费 100 英镑,支付 50 英镑 预订 2 - 收费 20 英镑,支付 10 英镑

我认为这是因为它在使用 MAX 和 MIN 时返回最高和最低。如何将返回到“HourlyAM”列的 MAX 和 MIN 值关联起来?

希望我解释得很好!谢谢

4

1 回答 1

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

这里的想法是,您根据一个字段为满足您的条件的每一行分配一个排名,然后根据该排名返回整行。

于 2013-06-21T16:09:25.910 回答