简体   繁体   中英

Arguments used in the DECODE function in ORACLE

Per my understanding the DECODE function is used to check the condition with the search expression and display the result. The below example has the DECODE function used with a group function called "SUM":

SELECT   COUNT(*) "Total",
        SUM(DECODE(TO_CHAR(hire_date,'YYYY'),'1995',1,0)) "1995",
        SUM(DECODE(TO_CHAR(hire_date,'YYYY'),'1996',1,0)) "1996",
        SUM(DECODE(TO_CHAR(hire_date,'YYYY'),'1997',1,0)) "1997",
        SUM(DECODE(TO_CHAR(hire_date,'YYYY'),'1998',1,0)) "1998"
FROM     employees;

I am trying to understand how it functions with the "TO_CHAR" function and the "SUM" function.

  1. The TO_CHAR function used in the example converts the date data type to the character data type and compares the values with the search values( 1995, 1996, etc), and calculates the result.
  2. However, the SUM function can only be used with numeric data type.

Here are the questions:

Question 1: Can the DECODE function also take the values that have different data types?

For instance, if the years are used as 1995, 1996 instead '1995', '1996', then the query gets executed successfully.

Question 2: The arguments "1" and "0" are numeric and used in the TO_CHAR function, and which is inside the group function "SUM". How does it execute when the data types in the DECODE clause are different?

Please help.

Question 1: Can the DECODE function also take the values that have different data types?

For instance, if the years are used as 1995, 1996 instead '1995', '1996', then the query gets executed successfully.

Not exactly, but it does allow implicit conversion. TO_CHAR is producing a string value, and so comparing with the string '1995' is fine. If the second argument was the number 1995 instead then the TO_CHAR result would be implicitly converted to a number for comparison - which is also valid with that that date format model, since the year is a number.

If you had a DECODE with more comparisons then the data types of each search expression and result value would need to match, but you're only doing one comparison here. And the SUM only see either 0 or 1.

Question 2: The arguments "1" and "0" are numeric and used in the TO_CHAR function, and which is inside the group function "SUM". How does it execute when the data types in the DECODE clause are different?

The 1 and 0 are not part of the TO_CHAR function, they are the third and fourth arguments to the DECODE. So the result of each DECODE is either 0 or 1, regardless of the data type of the first two arguments used for the comparison. The SUM has no visibility of the value being decoded or the search expressions, or their data types.

Perhaps it would help to break the nested function calls down a bit:

SUM(
  DECODE(
    TO_CHAR(hire_date,'YYYY'),  -- gets a string like '1995'
      '1995',                   -- if the string is '1995' then
        1,                      -- then value is 1
        0                       -- else value is 0
    )                           -- end of decode, result is 0 or 1
  )                             -- end of sum of 0/1 values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM