Differences in case and decode usage and Performance Comparison of Oracle Functions

Source: Internet
Author: User

In the Oracle world, you can use:

1) case expression or

2) decode Function

To realize logical judgment. Oracle's DECODE function is very powerful. If you use it flexibly, you can avoid multiple scans to improve query performance. CASE is a syntax provided after 9i. This syntax is more flexible and provides the if then else function.

Case expression

A case expression can be divided into two types: simple and search. A simple case is followed by an expression, for example:

Pay attention to the following points for a simple case:

1) Priority of when search: top to bottom

2) when there are more when, there is only one exit, that is, if one of them meets expr, it will immediately exit the case.

3) return_expr and else_expr cannot be specified as null, and the Data Types of expr, comparison_expr, and return_expr must be the same.

Search case:

Case when condition THEN return_expr

[WHEN condition THEN return_expr]

...

ELSE else_expr

END

Example:

  1. SELECT(CASE WHENCust_credit_limitBETWEEN0AND3999THEN'0-3999'
  2. WHENCust_credit_limitBETWEEN4000AND7999THEN '2017-1000'
  3. WHENCust_credit_limitBETWEEN8000AND11999THEN'2017-1000'
  4. WHENCust_credit_limitBETWEEN12000AND16000THEN '2017-1000' END)
  5. ASBUCKET,COUNT(*)ASCount_in_Group
  6. FROMMERsWHERECust_city ='Marshal' GROUP BY
  7. (CASE WHENCust_credit_limitBETWEEN0AND3999THEN '0-3999'
  8.  WHENCust_credit_limitBETWEEN4000AND7999THEN '2017-1000'
  9.  WHENCust_credit_limitBETWEEN8000AND11999THEN'2017-1000'
  10.  WHENCust_credit_limitBETWEEN12000AND16000THEN '2017-1000' END);
  11. BUCKET COUNT_IN_GROUP
  12. ---------------------------
  13. 0-3999 8
  14. 4000-7999 7
  15. 8000-11999 7
  16. 12000-16000 1

Decode can violate the 3NF (the row cannot be further divided, the column cannot be further divided, and the column cannot be repeated): The column is repeated.

  1. Hr @ ORCL>Select*FromA;
  2. IDNAME
  3. --------------------
  4. 1
  5. 2 B
  6. 3 c
  7. 1
  8. Hr @ ORCL>Select Sum(Decode (id, 1, 1, 0) think,
  9. 2Sum(Decode (id, 2, 2, 0) water,
  10. 3Sum(Decode (id, 3, 3, 0) linshuibin
  11. 4FromA;
  12. THINK WATER LINSHUIBIN
  13. ------------------------------
  14. 2 2 3
  • 1
  • 2
  • 3
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.