首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oracle SQL语句、嵌套语句和运算符(万一)

Oracle SQL语句、嵌套语句和运算符(万一)
EN

Stack Overflow用户
提问于 2022-03-28 00:08:42
回答 1查看 31关注 0票数 0

如果可能的话,在AND语句中寻找嵌套CASE运算符的一些帮助。

这是我的密码:

代码语言:javascript
复制
/*TRANSPORT UNIT*/
define tu_len = 564;
define tu_wid = 366;
define tu_hgt = 302;
define tu_wgt = 16;
define tu_min = 3;

/*CONVEYABLE MAX*/
define cv_lenmn = 620;
define cv_widmn = 545;
define cv_hgtmn = 500;
define cv_wgtmn = 0;

/*CONVEYABLE MIN*/
define cv_lenmx = 240;
define cv_widmx = 180;
define cv_hgtmx = 100;
define cv_wgtmx = 16;

/*TEST CUBI*/
define len = 564;
define wid = 366;
define hgt = 302;
define wgt = 16;

define len2 = 620;
define wid2 = 545;
define hgt2 = 500;
define wgt2 = 16;


SELECT
case 
    when len1 <= &tu_len  and wid1 <= &tu_len   and hgt1 <= &tu_len then 'LWH' 
    when len1 <= &tu_len  and wid1 <= &tu_len   and hgt1 > &tu_len then 'LW' 
    when len1 <= &tu_len  and wid1 > &tu_len    and hgt1 <= &tu_len then 'LH' 
    when len1 > &tu_len   and wid1 <= &tu_len   and hgt1 <= &tu_len then 'WH' 
    when len1 <= &tu_len  and wid1 > &tu_len    and hgt1 > &tu_len then 'L'
    when len1 > &tu_len   and wid1 <= &tu_len   and hgt1 > &tu_len then 'W' 
    when len1 > &tu_len   and wid1 > &tu_len    and hgt1 <= &tu_len then 'H' 
    else 'X' end as LENC1,
case 
    when len1 <= &tu_wid  and wid1 <= &tu_wid   and hgt1 <= &tu_wid then 'LWH' 
    when len1 <= &tu_wid  and wid1 <= &tu_wid   and hgt1 > &tu_wid then 'LW' 
    when len1 <= &tu_wid  and wid1 > &tu_wid    and hgt1 <= &tu_wid then 'LH' 
    when len1 > &tu_wid   and wid1 <= &tu_wid   and hgt1 <= &tu_wid then 'WH' 
    when len1 <= &tu_wid  and wid1 > &tu_wid    and hgt1 > &tu_wid then 'L'
    when len1 > &tu_wid   and wid1 <= &tu_wid   and hgt1 > &tu_wid then 'W' 
    when len1 > &tu_wid   and wid1 > &tu_wid    and hgt1 <= &tu_wid then 'H' 
    else 'X' end  as WIDC1,
case 
    when len1 <= &tu_hgt  and wid1 <= &tu_hgt   and hgt1 <= &tu_hgt then 'LWH' 
    when len1 <= &tu_hgt  and wid1 <= &tu_hgt   and hgt1 > &tu_hgt then 'LW' 
    when len1 <= &tu_hgt  and wid1 > &tu_hgt    and hgt1 <= &tu_hgt then 'LH' 
    when len1 > &tu_hgt   and wid1 <= &tu_hgt   and hgt1 <= &tu_hgt then 'WH' 
    when len1 <= &tu_hgt  and wid1 > &tu_hgt    and hgt1 > &tu_hgt then 'L'
    when len1 > &tu_hgt   and wid1 <= &tu_hgt   and hgt1 > &tu_hgt then 'W' 
    when len1 > &tu_hgt   and wid1 > &tu_hgt    and hgt1 <= &tu_hgt then 'H' 
    else 'X' end  as HGTC1,
case
    when wgt1 <= &tu_wgt then 'P'
    else 'F' end as WGTC1,
case  /*DOES IT FIT WITHIN 240 AND 620*/
    when (len2 >= &cv_lenmn and len2 <= &cv_lenmx)      and (wid2 >= &cv_lenmn and wid2 <= &cv_lenmx)       and (hgt2 >= &cv_lenmn and hgt2 <= &cv_lenmx)   then 'LWH' 
    when (len2 >= &cv_lenmn and len2 <= &cv_lenmx)      and (wid2 >= &cv_lenmn and wid2 <= &cv_lenmx)       and (hgt2 < &cv_lenmn or hgt2 > &cv_lenmx)      then 'LW' 
    when (len2 >= &cv_lenmn and len2 <= &cv_lenmx)      and (wid2 < &cv_lenmn or wid2 > &cv_lenmx)          and (hgt2 >= &cv_lenmn and hgt2 <= &cv_lenmx)   then 'LH' 
    when (len2 < &cv_lenmn or len2 > &cv_lenmx)         and (wid2 >= &cv_lenmn and wid2 <= &cv_lenmx)       and (hgt2 >= &cv_lenmn and hgt2 <= &cv_lenmx)   then 'WH' 
    when (len2 >= &cv_lenmn and len2 <= &cv_lenmx)      and (wid2 < &cv_lenmn or wid2 > &cv_lenmx)          and (hgt2 < &cv_lenmn or hgt2 > &cv_lenmx)      then 'L'
    when (len2 < &cv_lenmn or len2 > &cv_lenmx)         and (wid2 >= &cv_lenmn and wid2 <= &cv_lenmx)       and (hgt2 < &cv_lenmn or hgt2 > &cv_lenmx)      then 'W' 
    when (len2 < &cv_lenmn or len2 > &cv_lenmx)         and (wid2 < &cv_lenmn or wid2 > &cv_lenmx)          and (hgt2 >= &cv_lenmn and hgt2 <= &cv_lenmx)   then 'H' 
    else 'X' end as LENC2,                                      
case /*DOES IT FIT WITHIN 180 AND 545*/                                         
    when (len2 >= &cv_widmn and len2 <= &cv_widmx)      and (wid2 >= &cv_widmn and wid2 <= &cv_widmx)       and (hgt2 >= &cv_widmn and hgt2 <= &cv_widmx)   then 'LWH' 
    when (len2 >= &cv_widmn and len2 <= &cv_widmx)      and (wid2 >= &cv_widmn and wid2 <= &cv_widmx)       and (hgt2 < &cv_widmn or hgt2 > &cv_widmx)      then 'LW' 
    when (len2 >= &cv_widmn and len2 <= &cv_widmx)      and (wid2 < &cv_widmn or wid2 > &cv_widmx)          and (hgt2 >= &cv_widmn and hgt2 <= &cv_widmx)   then 'LH' 
    when (len2 < &cv_widmn or len2 > &cv_widmx)         and (wid2 >= &cv_widmn and wid2 <= &cv_widmx)       and (hgt2 >= &cv_widmn and hgt2 <= &cv_widmx)   then 'WH' 
    when (len2 >= &cv_widmn and len2 <= &cv_widmx)      and (wid2 < &cv_widmn or wid2 > &cv_widmx)          and (hgt2 < &cv_widmn or hgt2 > &cv_widmx)      then 'L'
    when (len2 < &cv_widmn or len2 > &cv_widmx)         and (wid2 >= &cv_widmn and wid2 <= &cv_widmx)       and (hgt2 < &cv_widmn or hgt2 > &cv_widmx)      then 'W' 
    when (len2 < &cv_widmn or len2 > &cv_widmx)         and (wid2 < &cv_widmn or wid2 > &cv_widmx)          and (hgt2 >= &cv_widmn and hgt2 <= &cv_widmx)   then 'H' 
    else 'X' end  as WIDC2,                             
case /*DOES IT FIT WITHIN 100 AND 500*/                                         
    when (len2 >= &cv_hgtmn and len2 <= &cv_hgtmx)      and (wid2 >= &cv_hgtmn and wid2 <= &cv_hgtmx)       and (hgt2 >= &cv_hgtmn and hgt2 <= &cv_hgtmx)   then 'LWH' 
    when (len2 >= &cv_hgtmn and len2 <= &cv_hgtmx)      and (wid2 >= &cv_hgtmn and wid2 <= &cv_hgtmx)       and (hgt2 < &cv_hgtmn or hgt2 > &cv_hgtmx)      then 'LW' 
    when (len2 >= &cv_hgtmn and len2 <= &cv_hgtmx)      and (wid2 < &cv_hgtmn or wid2 > &cv_hgtmx)          and (hgt2 >= &cv_hgtmn and hgt2 <= &cv_hgtmx)   then 'LH' 
    when (len2 < &cv_hgtmn or len2 > &cv_hgtmx)         and (wid2 >= &cv_hgtmn and wid2 <= &cv_hgtmx)       and (hgt2 >= &cv_hgtmn and hgt2 <= &cv_hgtmx)   then 'WH' 
    when (len2 >= &cv_hgtmn and len2 <= &cv_hgtmx)      and (wid2 < &cv_hgtmn or wid2 > &cv_hgtmx)          and (hgt2 < &cv_hgtmn or hgt2 > &cv_hgtmx)      then 'L'
    when (len2 < &cv_hgtmn or len2 > &cv_hgtmx)         and (wid2 >= &cv_hgtmn and wid2 <= &cv_hgtmx)       and (hgt2 < &cv_hgtmn or hgt2 > &cv_hgtmx)      then 'W' 
    when (len2 < &cv_hgtmn or len2 > &cv_hgtmx)         and (wid2 < &cv_hgtmn or wid2 > &cv_hgtmx)          and (hgt2 >= &cv_hgtmn and hgt2 <= &cv_hgtmx)   then 'H'  
    else 'X' end  as HGTC2,
case
    when wgt2 <= &cv_wgtmx then 'P'
    else 'F' end as WGTC2
FROM (select &len as len1,&wid as wid1,&hgt as hgt1,&wgt as wgt1,&len2 as len2,&wid2 as wid2,&hgt2 as hgt2,&wgt2 as wgt2 from DUAL );

结果:

基于X结果,我认为这样的嵌套和操作符(和)和(和)是不合适的。

请告诉我是否应该工作,我错过了一些东西,或者有一个更合适的方法。

我没有能力运行PL/SQL

EN

回答 1

Stack Overflow用户

发布于 2022-03-28 06:12:59

我认为这样的嵌套和操作符(和)和(和)是不合适的。

为什么不行?实际上,并不重要,。您可以删除括号(如果这正是困扰您的地方),因为所有这些条件都是AND。如果有一些OR,那么就很重要了,但是--没有什么(有优先权说AND是在OR之前计算的,但是--如果您想玩它,那么就使用括号)。

因此,从我的角度&我是如何理解这个问题的,您的查询是可以的。

但是,您可以将它们放入子查询(或CTE)中,而不是定义替换变量。

代码语言:javascript
复制
with temp as
  (select &len1, &wid1, &hgt1, ...
   from dual
  )
select
case 
    when len1 <= &tu_len  and wid1 <= &tu_len   and hgt1 <= &tu_len then 'LWH' 
    ...
from temp
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71641233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档