简体   繁体   中英

using DECODE with FIRST ORDER_BY in an sql query

I have started studying SQL, and while browsing a couple of reading materials I stumbled on this query:

SELECT MAX(SALARY) KEEP (DENSE_RANK FIRST ORDER BY 
    DECODE (SALARY, NULL, NULL, MONTHLY_SAL) NULLS LAST)

I have tried reading up on what this means and have come up with the following (please excuse my long winded and noobish explanations):

  1. DECODE (SALARY, NULL, NULL, MONTHLY_SAL) NULLS LAST - will return MONTHLY_SAL unless SALARY is NULL. NULL salaries will also be listed last.

  2. MAX(SALARY) - will give the maximum salary that will result from (DENSE_RANK...)

So my questions are these:

  1. Will MAX(SALARY) only give one value? or will it return the whole all rows and display the max salary? (ie if I have 2 rows having salaries of 1000 and 2000, the result will be two rows having values of 2000)

  2. What is the "total" effect of DENSE_RANK FIRST ORDER BY DECODE (...)? I have only seen examples of ORDER BY and am not sure what will happen if this is coupled with DECODE.

thank you

1) yes, one value

2) when you omit KEEP (DENSE_RANK... you'll get the max salary of all records. With KEEP (DENSE_RANK... you'll get max salary out of all records which have the smallest monthly_sal and a non-null salary. The job of the ´DECODE´-expression is to exclude records with null salary.

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