简体   繁体   English

在db2中如何使用Case Between

[英]How to use Case When with Between in db2

I want to retrieve the ROLE from the table on the basis of SOA_VALUE. 我想基于SOA_VALUE从表中检索ROLE。 ie if the SOA_VALUE is in between 100 and 500 then ROLE should be RSM. 即,如果SOA_VALUE在100到500之间,则ROLE应该是RSM。 If SOA_VALUE is more than 500 then ROLE should be TMM. 如果SOA_VALUE大于500,则ROLE应该是TMM。 The SOA_VALUE is coming from front end. SOA_VALUE来自前端。

ROLE    CHANNEL    PLG     TRANTYPE     SOA_VALUE
ASM       GT       DETS    TRANSFER     0    
RSM       GT       DETS    TRANSFER     100  
TMM       GT       DETS    TRANSFER     500  

Ex: If the SOA_VALUE is 122,then role should come as RSM. 例如:如果SOA_VALUE为122,则角色应为RSM。

Please give me any suggestion/example query to retrieve the Role.Thanks. 请给我任何建议/示例查询以检索Role.Thanks。

Please try this: 请尝试以下方法:

 SELECT CHANNEL,PLG,TRANTYPE,SOA_VALUE
    FROM TABLE_X          
      WHERE ROLE = (CASE 
    WHEN (120 >= 100 AND 120 <=500) THEN 'RSM'
    WHEN 120 > 500 THEN 'TMM'
    ELSE NULL
                   end)

Replace 120 with the string value of the SOA_VALUE above. 用上面的SOA_VALUE的字符串值替换120。

Demo: 演示:

http://sqlfiddle.com/#!2/72d7f/3 http://sqlfiddle.com/#!2/72d7f/3

How about this: 这个怎么样:

with t(input_soa_value) as (values 121) -- simulates input value
select role from table_x where soa_value = (
  select max(soa_value) from table_x, t where soa_value < input_soa_value
)

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

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