2013-03-25 68 views
18

嗨聲明我只是選擇和偉大工程:如果選擇(ORACLE)

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%' 

現在我想打電話給兩列: 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 

,但我得到的行錯誤: ELSE 0 END作爲Issue_Division_Result

ORA-00920:無效的關係運算符:(

+0

這將無法正確處理GCRS.STREAM_NAME的NULL值,因爲在這種情況下INSTR()返回NULL。 – 2013-03-25 12:29:03

回答

33
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 -- 
+0

THX !!!現在我明白了! – 4est 2013-03-25 14:40:58

+0

不客氣!樂意效勞。 – Art 2013-03-25 14:45:17

+1

這只是挽救了我的婚姻。謝謝。 – 2016-10-04 20:29:56

5

如此簡單你可以在這裏使用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 
+0

如果兩個值都爲null,這可能不是OP期望/想要的(參見我對OP的評論),這將返回0。 – 2013-03-25 12:29:44

+0

@FrankSchmitt看到我的編輯 – Dhinakar 2013-03-25 12:34:10

0

使用該變量,Oracle不支持在沒有INTO的情況下的SQL。使用正確命名的變量,您的代碼無論如何都會更清晰易讀。