0

我的查询中有 2 行从表中选择最小值和最大值。最多只会返回 2 行,但是如果只返回 1 行,我不希望为第二行返回任何内容。

IE:

MAX(CASE WHEN bd.DayText = 'Tuesday' 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)
"TuesdayHourlyAM",

MIN(CASE WHEN bd.DayText = 'Tuesday' 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)
"TuesdayHourlyAM2",     

因此,如果返回 1 行,TuesdayHourlyAM2 应该返回 null。

有任何想法吗?

谢谢

4

1 回答 1

1

尝试将其包装在一个case语句中:

(case when count(*) > 1
      then MIN(CASE WHEN bd.DayText = 'Tuesday' 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)
 end) as TuesdayHourlyAM2
于 2013-06-21T14:59:51.993 回答