DECODE 函 數 DECODE 函 數 的 功 能 有 點 像 程 式 語 言 的 if-then-else 句 子 , 它 會 能 夠 根 據 資 料 不 同 的 值 , 傳 回 不 同 的 資 料 , 語 法 如 下 : 
DECODE(COLUMN_NAME
    ,VALUE1, SUBSTITUTE1
    ,VALUE2, SUBSTITUTE2
    , ...
    ,DEFAULT
)如 果 COLUMN_NAME 的 值 是 VALUE1 , 就 傳 回 SUBSTITUTE1 , VALUE2 , 就 傳 回 SUBSTITUTE2 , 如 此 類 推 , 如 果 以 上 的 值 都 不 是 , 就 傳 回 DEFAULT 。 其 實 DECODE 也 可 以 做 到 NVL 的 功 能 , 例 如 NVL(COMM, 0) 可 以 寫 成 : 
DECODE(COMM, NULL, 0, COMM)假 設 你 想 列 出 每 個 僱 員 的 部 門 名 稱 , 除 了 可 以 把 EMP 連 接 到 DEPT 外 , 還 可 以 用 DECODE 來 做 。 SQL> SELECT ENAME
  2      ,DECODE(DEPTNO
  3          ,10 ,'Accounting'
  4          ,20 ,'Research'
  5          ,30 ,'Sales'
  6          ,40 ,'Opeartions'
  7          ,'UNKNOWN'
  8      ) DEPARTMENT
  9   FROM EMP
 10   ;ENAME      DEPARTMENT
---------- -------------
SMITH      Research
ALLEN      Sales
WARD       Sales
JONES      Research
MARTIN     Sales
BLAKE      Sales
CLARK      Accounting
SCOTT      Research
KING       Accounting
TURNER     Sales
ADAMS      Research
JAMES      Sales
FORD       Research
MILLER     Accounting14 rows selected.

解决方案 »

  1.   

    不可以在group by 后使用,为什么发两次贴子?
      

  2.   

    The function DECODE compares its first argument to one or more search expressions, which are paired with result expressions. Any search or result expression can be null. If a search is successful, the corresponding result is returned. In the following example, if the column rating is null, DECODE returns the value 1000:SELECT DECODE(rating, NULL, 1000, 'C', 2000, 'B', 4000, 'A', 5000)
       INTO credit_limit FROM accts WHERE acctno = my_acctno;
      

  3.   

    可以在group by 后
    select 
        decode(a,'1','b',a)
        sum(d)
    from tablename
    where 
    group by 
    decode(a,'1','b',a)
      

  4.   

    其实你自己实践下就知道了,decode在sql语句关键字后面的地方引用。
      

  5.   

    select id,name
    from aa
    group by decode(sex,'m','id,name','w','name,id')
    不对呀高手指点!!!
    我想通过一个字段值,控制分组顺序。
      

  6.   

    你的SELECT中要有GROUP BY 的字段
    select id,name,decode(sex,'m','id,name','w','name,id')
    from aa
    group by ID,NAME,decode(sex,'m','id,name','w','name,id')
      

  7.   

    where 语句里如何使用decode呢。。
    比如我对name字段进行限制,a的情况下,加入这个条件,b的情况下不考虑nameselect id
    from test
    where
    id='1'
    and
    decode(sex,'a','name=''2''','b',true);
    这样写,又错误,,高手指点!!!
      

  8.   

    select id
    from test
    where
    id='1'
    and
    decode(sex,'a','name=''2''','b',true);'name=''2''' 这种用法是不对的,oracle只会认为是字符串而不是条件不用decode ,改为这样就可以满足要求了:
    select id
    from test
    where
    id='1'
    and
     ((sex='a' AND name='2')  or (sex= 'b'));
      

  9.   

    下面用一个常见的数据显示来说明decode函数的用法。就是成绩单的显示,这个是教学管理系统中最常见的。我想做开发的人员都遇到过这个,而且在大学期间也是常常接触成绩单,显示的是:姓名、语文、数学等        实现脚本如下(cjd.sql):--建表
    create table stud
    (
     sid  varchar2(10),
     kcbm  varchar2(10),
     cj  int
    );
    --插入测试数据
    insert into stud values('1','语文',80);
    insert into stud values('2','数学',90);
    insert into stud values('3','英语',100);
    commit;
    --创建视图,decode用法
    create or replace view cjd as
     select sid,
     decode(kcbm,'语文',cj,0) 语文,
     decode(kcbm,'数学',cj,0) 数学,
     decode(kcbm,'英语',cj,0) 英语
     from stud
     order by sid;
    --显示数据
    select * from cjd;执行过程如下:SQL> create table stud(sid varchar2(10),
      2  kcbm varchar2(10),
      3  cj int);表已创建。SQL> insert into stud values('1','语文',80);已创建 1 行。SQL> insert into stud values('2','数学',90);已创建 1 行。SQL> insert into stud values('3','英语',100);已创建 1 行。SQL> commit;提交完成。SQL> create or replace view cjd as
      2  select sid,
      3  decode(kcbm,'语文',cj,0) 语文,
      4  decode(kcbm,'数学',cj,0) 数学,
      5  decode(kcbm,'英语',cj,0) 英语
      6  from stud
      7  order by sid;视图已建立。SQL> select * from cjd;SID              语文       数学       英语                                     
    ---------- ---------- ---------- ----------                                     
    1                  80          0          0                                     
    2                   0         90          0                                     
    3                   0          0        100
      

  10.   

    decode(column1,value1,output1,value2,output2,output3)
    对column1列执行DECODE函数,如果column1有一个值为value1,那么将会用output1来代替当前值,如果column1有一个值为value2,那么将会用output2来代替当前值,如果column1中哪两个值都不是,那么就会用OUTPUT3来代替当前值。
      

  11.   

    create table det_200406 as select tel,mtacc,acc,
    sum(decode(pitem,'P00',fee,null)) as 月租费,
    sum(decode(pitem,'P01',fee,null)) as 来电显示费,
    sum(decode(pitem,'P02',fee,null)) as 短信通信费,
    sum(decode(pitem,'P05',fee,null)) as 秘书台198,
    sum(decode(pitem,'P06',fee,null)) as 本地话费,
    sum(decode(pitem,'P07',fee,null)) as 国内长途费,
    sum(decode(pitem,'P08',fee,null)) as 国际长途费,
    sum(decode(pitem,'P09',fee,null)) as 漫游基本费,
    sum(decode(pitem,'P10',fee,null)) as 漫游国内长途费,
    sum(decode(pitem,'P11',fee,null)) as 漫游国际长途费,
    sum(decode(pitem,'P12',fee,null)) as 国内IP话费,
    sum(decode(pitem,'P13',fee,null)) as 国际IP话费,
    sum(decode(pitem,'P14',fee,null)) as 其它费,
    sum(decode(pitem,'P15',fee,null)) as 滞纳金,
    sum(decode(pitem,'P16',fee,null)) as 虚拟网包月费,
    sum(decode(pitem,'P17',fee,null)) as 短信信息费,
    sum(decode(pitem,'P18',fee,null)) as 声讯台信息费,
    sum(decode(pitem,'P19',fee,null)) as 包月调整费,
    sum(decode(pitem,'P20',fee,null)) as IP基本费 from det_6 group by tel,mtacc,acc;
      

  12.   

    当使用group by 的时候,没有在group by 中用到的表列在select 部分出现的时候必须使用分组函数(如:avg(),count(*),max())