您好,我遗漏了一些东西,因为我的代码错误。
select * from ##ScheduleDetail SD
left join ##HolidayFilterTbl HF on SD.Scheduledate = HF.Testdate
where (ScheduleDate = testdate)
and
(Case
when HF.IsHoliday = 1 then (overtime = 1 and makeup = 0)
else
(overtime = 0 and Makeup = 0)
end
)
and
DOW = 5
order by ActivityStartTime
我尝试了几种组合,每个组合都在第一个等号或第二个等号处出错。我错过了什么?
最佳答案
case
的分支表达式只能返回值,不能在 where
中计算额外的表达式健康)状况。但是,您可以使用 and
模拟这种行为。和 or
逻辑运算符:
select *
from ##ScheduleDetail SD
left join ##HolidayFilterTbl HF on SD.Scheduledate = HF.Testdate
where (ScheduleDate = testdate) and
((HF.IsHoliday = 1 and overtime = 1 and makeup = 0) or
(overtime = 0 and Makeup = 0)) and
DOW = 5
order by ActivityStartTime
请注意,您有 makeup = 0
在 case
的两个分支上问题中的表达式(或答案中 or
的两侧),因此您可以从中提取它并稍微简化条件:select *
from ##ScheduleDetail SD
left join ##HolidayFilterTbl HF on SD.Scheduledate = HF.Testdate
where ScheduleDate = testdate and
makeup = 0 and
((HF.IsHoliday = 1 and overtime = 1) or
overtime = 0) and
DOW = 5
order by ActivityStartTime
关于sql - 在 where 子句中使用 case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65116090/