Home Database Mysql Tutorial Oracle Decode函数详解

Oracle Decode函数详解

Jun 07, 2016 pm 05:18 PM
oracle oracle function

DECODE函数是ORACLE PL/SQL的功能强大的函数之一,目前还只有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能

select a.ok 合格情况,
sum(decode(cname, '计算机', countnum, 0)) 人数
from
(select
    cname,
    case when scgrade          when scgrade >=60 and scgrade          when scgrade >=70 and scgrade          when scgrade >=85 and scgrade          as ok,
    count(scgrade) as countnum
    from sc, c
    where sc.cno = c.cno
    group by
          cname,
          case when scgrade                when scgrade >=60 and scgrade                when scgrade >=70 and scgrade                when scgrade >=85 and scgrade group by a.ok
order by a.ok desc   

函数介绍

DECODE函数是Oracle PL/SQL的功能强大的函数之一,目前还只有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能。DECODE有什么用途呢? 先构造一个例子,假设我们想给智星职员加工资,其标准是:工资在8000元以下的加20%;工资在8000元或以上的加15%,通常的做法是,先选出记录中的工资字段值? select salary into var-salary from employee,然后对变量var-salary用if-then-else或choose case之类的流控制语句进行判断。 如果用DECODE函数,那么我们就可以把这些流控制语句省略,通过SQL语句就可以直接完成。如下:select decode(sign(salary - 8000),>=0,salary*1.15,

关于DECODE
  DECODE是Oracle公司独家提供的功能,它是一个功能很强的函数。它虽然不是SQL的标准,但对于性能非常有用。到目前,其他的数据库供应商还不能提供类似DECODE的功能,甚至有的数据库的供应商批评Oracle的SQL不标准。实际上,这种批评有些片面或不够水平。就象有些马车制造商抱怨亨利。福特的“马车”不标准一样。
1 DECODE 中的if-then-else逻辑
  在逻辑编程中,经常用到If – Then –Else 进行逻辑判断。在DECODE的语法中,实际上就是这样的逻辑处理过程。它的语法如下:
  DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )
  Value 代表某个表的任何类型的任意列或一个通过计算所得的任何结果。当每个value值被测试,如果value的值为if1,Decode 函数的结果是then1;如果value等于if2,Decode函数结果是then2;等等。事实上,可以给出多个if/then 配对。如果value结果不等于给出的任何配对时,Decode 结果就返回else 。
  需要注意的是,这里的if、then及else 都可以是函数或计算表达式。
2 DECODE 的简单例子
  Oracle系统中就有许多数据字典是使用decode 思想设计的,比如记录会话信息的V$SESSION数据字典视图就是这样。我们从《Oracle8i/9i Reference》资料中了解到,当用户登录成功后在V$SESSION中就有该用户的相应记录,但用户所进行的命令操作在该视图中只记录命令的代码(0—没有任何操作,2—Insert…),而不是具体的命令关键字。因此,我们需要了解当前各个用户的名字及他们所进行的操作时,要用下面命令才能得到详细的结果:
  select sid,serial#,username,
  DECODE(command,
  0,’None’,
  2,’Insert’,
  3,’Select’,
  6,’Update’,
  7,’Delete’,
  8,’Drop’,
  ‘Other’) cmmand
  from v$session where username is not null;
3 DECODE实现表的转置
  数据库中的表是由列和行构成的一个二维表。一般列在任何数据库中都是有限的数量,而行的变化较大,如果表很大,行的数量可能大上千万行。同一列的不同行可能有不同的值,而且不是预先定义的。
  例:住房公积金报表置换实例:
  1.各个单位在本地经办行进行开户,开户就是将单位的基本信息和职工信息的进行登记;
  2.每月各个单位的会计到经办行交缴本单位的所有职工的住房公积金,系统记录有每个职工的交缴明细并在每条记录上记录有经办行的代码;
  3.每月、季、半年及年终都要求将经办行 变为“列”给出个月的明细报表:
  经办行:城西区 城东区
  月份:
  2001.01 xxxx1.xx xxxxx2.xx
  2001.02 xxxx3.xx xxxxx4.xx
  。 。 。 。 。 。
  原来的数据顺序是:
  城西区2001.01 xxxxx1.xx
  城东区2001.01 xxxxx2.xx
  城西区2001.02 xxxxx3.xx
  城东区2001.02 xxxxx4.xx
  住房公积金系统记录职工的每月交缴名细的pay_lst表结构是:
  bank_code varchar2(6)NOT NULL, -- 经办行代码
  acc_no varchar2(15) not null, -- 单位代码(单位帐号)
  emp_acc_no varchar2(20) not null, -- 职工帐号
  tran_date date not null, -- 交缴日期
  tran_val Number(7,2) not null, -- 交缴额
  sys_date date default sysdate, --系统日期
  oper_id varchar2(10) --操作员代码
  这样的表结构,一般按照将经办行作为行(row)进行统计是很容易的,但是如果希望将经办行变为列(column)这样的格式来输出就有困难。如果用DECODE函数来处理则变得很简单:
  我们创建一个视图来对目前的pay_lst表进行查询。将经办行代码变为一些具体的经办行名称即可:
  CREATE OR REPLACE VIEW bank_date_lst AS
  Select to_char(tran_date,’yyyy.mm’),
  SUM( DECODE ( bank_code,’001’, tran_val,0 )) 城西区,,
  SUM( DECODE ( bank_code,’002’, tran_val,0 )) 城南区,
  SUM( DECODE ( bank_code,’003’, tran_val,0 )) 城东区
  FROM pay_lst
  GROUP BY to_char(tran_date,’yyyy.mm’);
  建立视图后,可直接对该视图进行查询就可按照列显示出结果。

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

See all articles