简体   繁体   English

在 where 子句中使用 case 语句

[英]using case statement in a where clause

Hello I am missing something because my code errors.你好,我遗漏了一些东西,因为我的代码错误。

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

I've attempted several combinations and each one errors at either the first equal sign or the second.我尝试了几种组合,每一种组合都在第一个等号或第二个等号处出错。 What am I missing?我错过了什么?

The branches of a case expression can only return values, not additional expressions to be evaluated in the where condition. case表达式的分支只能返回值,不能返回where条件中要计算的附加表达式。 You could, however, simulate this behavior with the and and or logical operators:但是,您可以使用andor逻辑运算符模拟此行为:

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

Note that you have makeup = 0 on both branches of the case expression in the question (or both sides of the or in the answer), so you could extract it out of it and simplify the condition a bit:请注意,问题中case表达式的两个分支(或答案中or的两侧)都有makeup = 0 ,因此您可以将其提取出来并稍微简化条件:

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

If you are still wanting to know how to utilize a CASE Statement Expression in a WHERE Clause the CASE Expression must be compared to a value as that is the syntax understood for conditions contained within a WHERE Clause.如果您仍然想知道如何在 WHERE 子句中使用 CASE 语句 表达式,则必须将 CASE 表达式与一个值进行比较,因为这是理解 WHERE 子句中包含的条件的语法。 See below a mock example.请参阅下面的模拟示例。

SELECT *
FROM ##ScheduleDetail SD
     LEFT JOIN ##HolidayFilterTbl HF ON SD.Scheduledate = HF.Testdate
WHERE(ScheduleDate = testdate)
     AND
     /* If you wish to stick with using a CASE Expression within the WHERE Clause set the the CASE Expression equal to 'something'.  I usually stick with 1 or 0 'true/false'.
      |  You simply have to create your own True/False evaluation.  You can add your logic checks within a CASE Expression within
      |  the WHERE Clause and when your logic is TRUE THEN return 1.  Basically you are saying when 1 = 1 then return Record.
     */
     1 = 
     Case 
      WHEN HF.IsHoliday = 1 AND makeup = 0 THEN
        CASE WHEN (overtime = 1 OR overtime = 0) THEN 1 END /* Return 1 here to evaluation to TRUE */
      ELSE      
        0 /* You can add another CASE here if needed and when the condition you write in evaluations to 1 'true' return record */
      END
     
     AND
     DOW = 5

ORDER BY ActivityStartTime;

There are a few reasons I've used CASE Expressions within a WHERE Clause over using AND/ORs.我在 WHERE 子句中使用 CASE 表达式而不是使用 AND/OR 有几个原因。 Just one minor reason is it allows me to contain and organize logic in a WHERE Clause inside CASE Expressions rather than having multiple AND/ORs all nested together.一个次要的原因是它允许我在 CASE 表达式内的 WHERE 子句中包含和组织逻辑,而不是将多个 AND/OR 全部嵌套在一起。 I've also found that using CASE Expressions in the WHERE Clause is useful when encountering Dynamic queries that accept variables to be later inserted into the SQL before being sent to the database for processing.我还发现,在遇到接受变量的动态查询时,在 WHERE 子句中使用 CASE 表达式很有用,这些变量稍后将插入到 SQL 中,然后再发送到数据库进行处理。 In the case of using Dynamic SQL there are times when a CASE Statement MUST be used due to the fact that there could be data that is being compared against in the WHERE clause that is NOT a column.field value but a hardcoded value that is compared to perhaps a user selection or status (as examples)... it might be a static value passed in via the application which is how my web application works that I support which is why I bring it up.在使用 Dynamic SQL 的情况下,有时必须使用 CASE 语句,因为 WHERE 子句中可能存在正在比较的数据,该数据不是 column.field 值,而是比较的硬编码值可能是用户选择或状态(例如)...它可能是通过应用程序传入的 static 值,这就是我支持的 web 应用程序的工作方式,这就是我提出它的原因。

Basically it's good to know how to use a CASE Expression in a WHERE Clause as there are some cases when the ONLY way to evaluate certain data is by using a CASE Expression.基本上,了解如何在 WHERE 子句中使用 CASE 表达式是件好事,因为在某些情况下,评估某些数据的唯一方法是使用 CASE 表达式。

I have no data to test this against and that's not the point.我没有数据可以对此进行测试,这不是重点。 The point of my answer is to simply provide to you an alternative to the existing answer.我的回答的重点是简单地为您提供现有答案的替代方案。 In my opinion this logic is basic and the already provided answer is the correct one however my answer is to demonstrate how you could go about using a CASE in a WHERE Clause.在我看来,这个逻辑是基本的,并且已经提供的答案是正确的,但是我的答案是为了演示 go 如何在 WHERE 子句中使用 CASE。

If interested see this SO Post for the differences between a CASE Statement vs a CASE Expression however know that this terminology slightly differs between databases.如果有兴趣查看SO 帖子以了解 CASE 语句与 CASE 表达式之间的差异,但是要知道该术语在数据库之间略有不同。

As an example of this... SQL Server refers to these as Simple vs Searched but refers to all of it as a CASE Expression.作为一个例子... SQL 服务器将这些称为简单与搜索,但将所有这些称为 CASE 表达式。 Therefore a CASE Expression can either be a Simple or a Searched CASE that can be used within a Statement.因此,CASE 表达式可以是可在语句中使用的简单 CASE 或搜索 CASE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM