decode 的用法

今天在论坛上见到有这方面的需求,可以采用decode 和case when来进行解决

create table tstconn (c1 varchar2(5), c2 varchar2(5));
insert into tstconn values(0, 1);
insert into tstconn values(1, 2);
insert into tstconn values(2, 3);
commit;

SQL> select * from tstconn;

C1 C2
----- -----
0 1
1 2
2 3
SQL> select decode(max(c1)-max(c2),0,1,max(c1),2,max(c2)) max,
2 decode(min(c1)-min(c2),0,1,min(c2),2,min(c1)) min
3 from tstconn;

MAX MIN
---------- ----------
3 0

另外也可以采用 select case 来实现,这里就不在列出例子了

The CASE expression made its SQL debut in the SQL-92 specification in 1992. Eight years later, Oracle included the CASE expression in the 8.1.6 release. Like the DECODE function, the CASE expression enables conditional logic within an SQL statement, which might explain why Oracle took so much time implementing this particular feature. If you have been using Oracle for a number of years, you might wonder why you should care about the CASE expression, since DECODE does the job nicely. Here are several reasons why you should make the switch:

CASE expressions can be used everywhere that DECODE functions are permitted.

CASE expressions are more readable than DECODE expressions.

CASE expressions execute faster than DECODE expressions.[1]

[1] Since CASE is built into Oracle's SQL grammar, there is no need to call a function in order to evaluate the if-then-else logic. While the difference in execution time is miniscule for a single call, the aggregate time savings from not calling a function should become noticeable when working with large result sets.

CASE expressions handle complex logic more gracefully than DECODE expressions.

CASE is ANSI-compliant, whereas DECODE is proprietary.

The only downside to using CASE over DECODE is that CASE expressions are not supported in Oracle8i's PL/SQL language. If you are using Oracle9i, however, any SQL statements executed from PL/SQL may include CASE expressions.

The SQL-92 specification defines two distinct flavors of the CASE expression: searched and simple. Searched CASE expressions are the only type supported in the Oracle8i release. If you are using Oracle9i, you may also use simple CASE expressions.

9.2.1 Searched CASE Expressions
A searched CASE expression evaluates a number of conditions and returns a result determined by which condition is true. The syntax for the SEARCHED CASE expression is as follows:

CASE

WHEN C1 THEN R1

WHEN C2 THEN R2

...

WHEN CN THEN RN

ELSE RD

END
In the syntax definition, the "C"s represent conditions, and the "R"s represent results. You can use up to 127 WHEN clauses in each CASE expression, so the logic can be quite robust. Conditions are evaluated in order. When a condition is found that evaluates to TRUE, the corresponding result is returned, and execution of the CASE logic ends. Therefore, carefully order WHEN clauses to ensure that the desired results are achieved. The next example illustrates the use of the CASE statement by determining the proper string to show on an order status report:

SELECT co.order_nbr, co.cust_nbr,

CASE WHEN co.expected_ship_dt IS NULL THEN 'NOT YET SCHEDULED'

WHEN co.expected_ship_dt <= SYSDATE THEN 'SHIPPING DELAYED'

WHEN co.expected_ship_dt <= SYSDATE + 2 THEN 'SHIPPING SOON'

ELSE 'BACKORDERED'

END ship_status

FROM cust_order co

WHERE co.ship_dt IS NULL AND co.cancelled_dt IS NULL;
Similar to DECODE, all results in the CASE expression must have comparable types; otherwise, ORA-932 will be thrown. Each condition in each WHEN clause is independent of the others, however, so your conditions can include various data types, as demonstrated in the next example:

SELECT co.order_nbr, co.cust_nbr,

CASE

WHEN co.sale_price > 10000 THEN 'BIG ORDER'

WHEN co.cust_nbr IN

(SELECT cust_nbr FROM customer WHERE tot_orders > 100)

THEN 'ORDER FROM FREQUENT CUSTOMER'

WHEN co.order_dt < TRUNC(SYSDATE) -- 7 THEN 'OLD ORDER'

ELSE 'UNINTERESTING ORDER'

END

FROM cust_order co

WHERE co.ship_dt IS NULL AND co.cancelled_dt IS NULL;
9.2.2 Simple CASE Expressions
Simple CASE expressions are structured differently than searched CASE expressions in that the WHEN clauses contain expressions instead of conditions, and a single expression to be compared to the expressions in each WHEN clause is placed in the CASE clause. Here's the syntax:

CASE E0

WHEN E1 THEN R1

WHEN E2 THEN R2

...

WHEN EN THEN RN

ELSE RD

END
Therefore, each of the expressions E1...EN are compared to expression E0. If a match is found, the corresponding result is returned; otherwise, the default result (RD) is returned. As a result, all of the expressions must be of the same type, since they all must be compared to E0, making simple CASE expressions less flexible than searched CASE expressions. The next example illustrates the use of a simple CASE expression to translate the status code stored in the part table:

SELECT p.part_nbr part_nbr, p.name part_name, s.name supplier,

CASE p.status

WHEN 'INSTOCK' THEN 'In Stock'

WHEN 'DISC' THEN 'Discontinued'

WHEN 'BACKORD' THEN 'Backordered'

WHEN 'ENROUTE' THEN 'Arriving Shortly'

WHEN 'UNAVAIL' THEN 'No Shipment Scheduled'

ELSE 'Unknown'

END part_status

FROM part p, supplier s

WHERE p.supplier_id = s.supplier_id;
A searched CASE can do everything that a simple CASE can do, which is probably the reason Oracle only implemented searched CASE expressions the first time around. For certain uses, such as translating values for a column, simple expressions may prove more efficient if the expression being evaluated is computed via a function call.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/39335/viewspace-351722/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/39335/viewspace-351722/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值