Com*_*ard 4 sql select case-statement where-clause
您好,由于我的代码错误,我丢失了一些东西。
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
Run Code Online (Sandbox Code Playgroud)
我尝试了几种组合,但每种组合都在第一个等号或第二个等号处出错。我缺少什么?
表达式的分支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
Run Code Online (Sandbox Code Playgroud)
请注意,问题中表达式makeup = 0的两个分支(或答案中表达式的两侧)都有,因此您可以将其从中提取出来并稍微简化条件:caseor
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21305 次 |
| 最近记录: |