2013-11-26 51 views
1

我有一個遊標有多個條件來檢索一組數據。我最近添加日期條件在用戶輸入如下它來檢索特定日期之間的數據:在where子句中使用if-else/decode

cursor c1 
    is 
    select t.* 
     from (select v.*, dense_rank() over (order by created desc) 
                    as rank 
       from test.table_v2 v 
       where some condition 
       and some condition 
        and some condition 
        and some condition 
        and some condition 
        and some condition 
       and ((ended) between to_date(sd,'mm/dd/rrrr') 
            and to_date(sd,'mm/dd/rrrr')+3 
        or ended like decode(sd,null,'%')) --new condition 
       and some condition 
       ) t 
     where rank < rmax+1 
    order by ended desc; 

這裏RMAX = 1000

我需要在where子句中添加一個條件,以便當sd(用戶輸入的日期)爲空,行數限制爲1000,當其不爲空時,不應考慮行限制。

我不知道是否可以在where子句中使用decode。有沒有辦法做到這一點?

回答

1

只需添加or sd is [not] null

select t.* 
    from (select v.*, dense_rank() over (order by created desc) 
                   as rank 
      from test.table_v2 v 
      where some condition 
      and some condition 
       and some condition 
       and some condition 
       and some condition 
       and some condition 
      and (ended between to_date(sd,'mm/dd/rrrr') 
          and to_date(sd,'mm/dd/rrrr')+3 
       or sd is null) --new condition 
      and some condition 
      ) t 
    where rank < rmax+1 or sd is not null 
+0

不能相信我做到了我的頭那麼複雜。謝謝@Jeffrey Kemp – amsko