2014-02-14 72 views
-1

感謝stackoverflow社區,我發現由於CASE WHEN,Oracle 8i不支持此查詢。Oracle 8i中CASE的替代方案

select (case 
     when seqnum = 1 then 
      '1' 
     when seqnum = cnt then 
      '0' 
     end) as value1, 
     (case 
     when seqnum = 1 then 
      t.BEGIN_DT 
     when seqnum = cnt then 
      t.END_DT 
     end) as TIME1,  
     t4.UNIT1 || '.SUBBATCH_TRIGGER' TAG  
    from (select t.*, 
       row_number() over(partition by t.BATCH_ID, t.plant_unit, t3.ID2 order by t.BEGIN_DT) as seqnum, 
       count(*) over(partition by t.BATCH_ID, t.plant_unit, t3.ID2) as cnt 
      from SCH2.tb_pg_unit_stap t 
join (select ID1,batch_id from SCH2.VW_BATCH) t2 on t.BATCH_ID = t2.BATCH_ID 
join (select ID2,ID1 from SCH1.STEP) t3 on t3.ID1 = t2.ID1) t 
join SCH2.TB_W_MACHINE t4 on t4.plant_unit = t.plant_unit 
where (seqnum = 1 
    or seqnum = cnt) AND (t.BEGIN_DT > '01-jan-2013' AND t.BEGIN_DT < '01-feb-2013'); 

此查詢在Oracle 8i上運行有哪些替代方法?

在此先感謝!

+2

你也將運行與ANSI語法和解析函數的麻煩。 –

回答

5

你應該decode(..., ..., ...)

select 
    decode(seqnum, 
      1 , '1', 
      cnt, '0' 
     ) as     value1, 
    decode(seqnum, 
      1 , t.BEGIN_DT, 
      cnt, t.END_DT 
) as       TIME1 
    ... 

here's the link嘗試解碼的文檔。

但是,正如已經在評論中指出的,join構造(ansi加入)在8i中也不起作用。

+0

對不起,我只是一個試圖從Oracle獲取數據的.NET開發人員:)我應該在哪裏使用解碼(...,...,...)?對不起,這個愚蠢的問題 –

+1

@Dalek:我給了你一個例子。這有幫助嗎? –

+0

它幫助了很多。謝謝! –

2

,而不是(case when seqnum = 1 then '1' when seqnum = cnt then '0' end) 你可以使用decode(seqnum, 1, '1', decode(seqnum, cnt, '0'))

我希望你的想法