如果语句在select(ORACLE)

4es*_*est 37 sql oracle select if-statement oracle10g

嗨,我只是选择并且工作得很好:

select 'CARAT Issue Open' issue_comment, i.issue_id, i.issue_status, i.issue_title, i.ISSUE_summary ,i.issue_description, i.severity,
gcrs.Area_name, gcrs.sector_name,

substr(gcrs.stream_name,1,case when instr(gcrs.stream_name,' (')=0 then 100 else instr(gcrs.stream_name,' (')-1 end) ISSUE_DIVISION,

case when gcrs.STREAM_NAME like 'NON-GT%' THEN 'NON-GT' ELSE gcrs.STREAM_NAME END as ISSUE_DIVISION_2


from table(f_carat_issues_as_of('31/MAR/2013')) i
inner join v_gcrs_with_stream gcrs on i.segment_id = gcrs.segment_id

where UPPER(ISSUE_STATUS) like '%OPEN%'
Run Code Online (Sandbox Code Playgroud)

现在我想调用两列:ISSUE_DIVISION和ISSUE_DIVISION_2

如果它们在新列中相等则应为值1,如果不相等则应为0,

我该怎么做 ?


我的完整代码:

select 'CARAT Issue Open' issue_comment, i.issue_id, i.issue_status, i.issue_title, i.ISSUE_summary ,i.issue_description, i.severity,
gcrs.Area_name, gcrs.sector_name,

substr(gcrs.stream_name,1,case when instr(gcrs.stream_name,' (')=0 then 100 else instr(gcrs.stream_name,' (')-1 end) ISSUE_DIVISION,

case when gcrs.STREAM_NAME like 'NON-GT%' THEN 'NON-GT' ELSE gcrs.STREAM_NAME END as ISSUE_DIVISION_2


from table(f_carat_issues_as_of('31/MAR/2013')) i
inner join v_gcrs_with_stream gcrs on i.segment_id = gcrs.segment_id

where UPPER(ISSUE_STATUS) like '%OPEN%' and 

CASE WHEN ISSUE_DIVISION = ISSUE_DIVISION_2 THEN 
     CASE WHEN  ISSUE_DIVISION is null then "Null Value found"
     Else 1 End
ELSE 0 END As Issue_Division_Result
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误:ELSE 0 END As Issue_Division_Result

ORA-00920:无效的关系运算符:(

Art*_*Art 59

SELECT (CASE WHEN ISSUE_DIVISION = ISSUE_DIVISION_2 THEN 1 ELSE 0 END) AS ISSUES
    --  <add any columns to outer select from inner query> 
  FROM
 (  -- your query here --
   select 'CARAT Issue Open' issue_comment, ...., ..., 
          substr(gcrs.stream_name,1,case when instr(gcrs.stream_name,' (')=0 then 100 else  instr(gcrs.stream_name,' (')-1 end) ISSUE_DIVISION,
          case when gcrs.STREAM_NAME like 'NON-GT%' THEN 'NON-GT' ELSE gcrs.STREAM_NAME END as ISSUE_DIVISION_2
     from ....
    where UPPER(ISSUE_STATUS) like '%OPEN%'
 )
 WHERE... -- optional --
Run Code Online (Sandbox Code Playgroud)

  • 这只是救了我的婚姻.谢谢. (17认同)
  • 有时 PL/SQL 试图破坏任何人的婚姻!哈哈哈 (2认同)

Dhi*_*kar 11

这么简单,你可以在这里使用case语句.

CASE WHEN ISSUE_DIVISION = ISSUE_DIVISION_2 THEN 
         CASE WHEN  ISSUE_DIVISION is null then "Null Value found" //give your option
         Else 1 End
ELSE 0 END As Issue_Division_Result
Run Code Online (Sandbox Code Playgroud)

  • 如果两个值都为 null,这将返回 0,这可能不是 OP 期望/想要的(请参阅我对 OP 的评论)。 (2认同)