简体   繁体   English

在Where子句SQL中使用Case

[英]use Case in Where Clause SQL

Hi to make this short. 嗨,做这个简短。

I need only the column when a condition is met. 当条件满足时我只需要列。 if the condition is not met, then I can proceed running my Query without that column as validation. 如果条件不满足,那么我可以继续运行我的查询而不将该列作为验证。

CONDITION NEEDED. 需要的条件。

REFERENCE TABLE - TABLE_A
inside TABLE_A there is PARAMETER_NAME AND PARAMETER_VALUE
REQUIRED: PARAMETER_NAME= 'SMS_SEND'
PARAMETER_VALUE = 'TRUE'

COLUMN FOR VALIDATION INSIDE TABLE_B INVALID_STAT ---> COLUMN 用于验证的栏目表_无效_STAT --->栏目

IF PARAMETER_NAME= 'SMS_SEND' and PARAMETER_VALUE = 'TRUE'
THEN I NEED TO CHECK INVALID_STAT IF ITS EQUAL TO 'Y'
ELSE
I DONT NEED TO VALIDATE INVALID_STAT AT ALL



WHERE (CASE
           WHEN 1 = (SELECT DECODE (PARAMETER_VALUE, 'FALSE', 1, 2)
                       FROM TABLE_A
                      WHERE PARAMETER_NAME = 'SMS_SEND')
           THEN
              INVALID_STAT
           ELSE
              'Y'
        END) = 'Y'

based upon your commentary, this will get you what you have asked for. 根据你的评论,这将得到你所要求的。

However, I doubt this is what you need. 但是,我怀疑这是你需要的。 Please update your question as requested in the commentary above. 请按照上述评论的要求更新您的问题。 If this does answer your question, please accept it as the answer and close the question. 如果这确实回答了您的问题,请接受它作为答案并关闭问题。

I expect that the real requirement will be to wrap the following query in an outer join to a larger query (which you have not supplied) along with a coalesce to deal with the possible no rows scenario. 我希望真正的要求是将外部联接中的以下查询包装到更大的查询(您尚未提供)以及合并以处理可能的无行方案。


select 
  case
    when PARAMETER_VALUE = 'FALSE' THEN 'Y'
    else ''
  end as SMS_Send_Ind
from Table_A
where PARAMETER_Name = 'SMS_SEND'
;

If there is no record in the table with Parameter_name = 'SMS_SEND', then the query will return no rows. 如果表中没有带Parameter_name ='SMS_SEND'的记录,则查询将不返回任何行。 Otherwise it will a Y or a blank as determined by the number of records in Table_A with parameter_name = 'SMS_SEND' 否则,它将是Y或空白,由Table_A中带有parameter_name ='SMS_SEND'的记录数决定

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

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