WHERE子句中的CASE? [英] CASE in WHERE clause?
问题描述
WHERE
-
(b.is_ot_paycode = ' Y' 或
- 来自variableA
b.is_ot_paycode = ' Y')
< span class =code-keyword> AND
- 来自TABLE.PARAMETER1
(b.is_wh_paycode = ' Y' OR
- 来自variableA
b.is_wh_paycode = ' Y')
- 来自variableB
AND b.is_ot_paycode = ' Y'
如何在我的WHERE子句中创建一个CASE语句
当b.is_wh_paycode ='Y'(来自变量A)和
b.is_ot_paycode ='Y'(来自变量B)
我的b.is_wh_paycode (来自TABLE.PARAMETER1)总是等于'N'?
请和谢谢
< blockquote>如果我理解你的问题,下面会澄清你的问题。
选择 * 来自 TABLE1 其中( case
当标志= ' Y' 然后 1
else 0
end )> 0
在上面的查询中,Flag列将为1或0作为值。我们只使用case语句来提取具有flag = Y的记录。
只是将case视为返回SQL类型的函数。在谓语中,你总是会这样做:
其中f(x)< operator>表达
案例是f(x)
运算符是>在这种情况下
快递是0
凡案例......结束> 0
访问这里......
http://stackoverflow.com/questions/7606019/sql-server-using-case-in-where-clause [ ^ ]
或
http://stackoverflow.com/questions/8785209/case-statement-within-where-clause-in-sql-server-2008 [ ^ ]
WHERE
-- from TABLE.PARAMETER1
(b.is_ot_paycode = 'Y' OR
-- from variableA
b.is_ot_paycode = 'Y')
AND
-- from TABLE.PARAMETER1
(b.is_wh_paycode = 'Y' OR
-- from variableA
b.is_wh_paycode = 'Y')
-- from variableB
AND b.is_ot_paycode = 'Y'
How can I create a CASE statement in my WHERE clause that
when b.is_wh_paycode = 'Y' (from variableA) and
b.is_ot_paycode = 'Y'(from variableB)
my b.is_wh_paycode (from TABLE.PARAMETER1) would always be equal to 'N'?
Please and Thank you
If i understand your question correctly, below would clarify your question.
select * from TABLE1 where (case when Flag='Y' then 1 else 0 end) > 0
In the above query, Flag column will have either 1 or 0 as value. We are pulling only the records that has flag=Y using case statements.
just think of case as a function that returns a SQL type. In a predicate, you would always do
where f(x) <operator> expression
case is the f(x)
operator is > in this case
express is 0
where case .... end > 0
visit here...
http://stackoverflow.com/questions/7606019/sql-server-using-case-in-where-clause[^]
or
http://stackoverflow.com/questions/8785209/case-statement-within-where-clause-in-sql-server-2008[^]
这篇关于WHERE子句中的CASE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!