我们可以在select子句中使用关系运算符吗?

时间:2012-12-05 14:21:41

标签: sql oracle11g

我想制作我的金额低于特定值的列。当我尝试这个时,查询运行

select LOSA_APP.app_ref_no AS "App.Ref.No.",
   LOSA_EXP_SUMM_Z.group_exp AS "Group Exposure Amount",
   LOSA_EXP_SUMM_Z.group_exp - 25000 AS "Less than",
   columns AS "some Name"
   columns AS "some Name"
from 
     losa_app LOSA_APP
INNER JOIN
    code_branch CODE_BRANCH
ON
    LOSA_APP.attend_branch = CODE_BRANCH.branch_id
....
where 
    LOSA_APP.app_status in ('A','R')
and
    ....
 or 
 (
    '&&aplication' = 'Enhancement' 
    and 
    (
        nvl(LOSA_APP.review_type, '-') IN ('Enhancement', 'Additional')  and nvl(LOSA_APP.review_freq, '-') IN ('Enhancement', 'New')
    )
);

但是当我将行LOSA_EXP_SUMM_Z.group_exp - 25000 AS "Less than"更改为LOSA_EXP_SUMM_Z.group_exp < 25000 AS "Less than"时,我会收到错误

ORA-00923: FROM keyword not found where expected
00923. 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:
Error at Line: 3 Column: 34

第3行是LOSA_EXP_SUMM_Z.group_exp < 25000 AS "Less than"。为什么在我们使用subtract sign时运行并在relational operator <?p>的情况下给出错误

由于

1 个答案:

答案 0 :(得分:1)

如果要在SELECT中使用关系运算符,则需要在类似于此的CASE语句中使用它:

select LOSA_APP.app_ref_no AS "App.Ref.No.",
   LOSA_EXP_SUMM_Z.group_exp AS "Group Exposure Amount",
   CASE 
        WHEN LOSA_EXP_SUMM_Z.group_exp < 25000 
        then 'true'
        else 'false' 
     end AS "Less than",
   columns AS "some Name"
   columns AS "some Name"
from losa_app LOSA_APP
INNER JOIN code_branch CODE_BRANCH
    ONLOSA_APP.attend_branch = CODE_BRANCH.branch_id
....
where  LOSA_APP.app_status in ('A','R')
and
    ....
 or 
 (
    '&&aplication' = 'Enhancement' 
    and 
    (
        nvl(LOSA_APP.review_type, '-') IN ('Enhancement', 'Additional')  and nvl(LOSA_APP.review_freq, '-') IN ('Enhancement', 'New')
    )
);

如果记录小于25000,现在将返回truefalse。您可以将回报更改为您需要的任何内容,包括:

select LOSA_APP.app_ref_no AS "App.Ref.No.",
   LOSA_EXP_SUMM_Z.group_exp AS "Group Exposure Amount",
   CASE WHEN LOSA_EXP_SUMM_Z.group_exp < 25000 
            then LOSA_EXP_SUMM_Z.group_exp - 25000
        else LOSA_EXP_SUMM_Z.group_exp end AS "Less than",
   columns AS "some Name"
   columns AS "some Name"
from losa_app LOSA_APP
INNER JOIN code_branch CODE_BRANCH
    ONLOSA_APP.attend_branch = CODE_BRANCH.branch_id
....
where  LOSA_APP.app_status in ('A','R')
and
    ....
 or 
 (
    '&&aplication' = 'Enhancement' 
    and 
    (
        nvl(LOSA_APP.review_type, '-') IN ('Enhancement', 'Additional')  and nvl(LOSA_APP.review_freq, '-') IN ('Enhancement', 'New')
    )
);