Decode statement to check for less than greater than sends an error

Hi,

Im using Oracle 10 g and sql talk for windows
The query can work with a case statement, but have to convert my SQL  to use the DECODE statement.  
I would like the decode to use logic like this:
if  (A.FIXED + B. RATE) is less than minrate,
then use minrate
else
if (A.FIXED + B. RATE) is greater than maxrate
then use maxtrate
else
use the rate from (A.FIXED + B. RATE)
the decode will run like the sql below, but this is not checking for less than or greater than
Decode((A.FIXED + B. RATE),
  A.MINRATE,  A. MINTRATE,
  A.MAXRATE,  A.MAXRATE,  (A.FIXED + B.RATE)) APR4


If i change the decode to include < or > (like below),  


Decode((A.FIXED + B. RATE),
  < A.MINRATE, - A. MINTRATE,
 >  A.MAXRATE,  A.MAXRATE,  (A.FIXED + B.RATE)) APR4


I receive an error
Error: ORA=00936 missing expression.

Below is the whole sql

SELECT
(A.FIXED + B. RATE)  APR1, A.MINRATE,
 A.MAXRATE,
B.RATE,
(SELECT DECODE(CY.LEAPYR, 'Y', 366, 365)   FROM CY CY WHERE YRNBR = (SELECT TO_CHAR( SYSDATE,'YYYY')  FROM DUAL)) AS DAYSINYEAR,
Decode((A.FIXED + B. RATE),
  A.MINRATE,  A. MINTRATE,
  A.MAXRATE,  A.MAXRATE,  (A.FIXED + B.RATE)) APR4
FROM
 A A ,
 B B
WHERE A.A = 'CODE'
AND A.DATE IS NULL
 AND B. NBR = A. NBR
 AND B. DATE2 = (SELECT MAX(DATE2)  FROM
TABLEA  WHERE NBR =  01);


Thank you from CJJA
CJJCAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
this will do better
CASE WHEN A.FIXED + B. RATE < A.MINRATE THEN A. MINTRATE
     WHEN A.FIXED + B. RATE > A.MAXRATE THEN A.MAXRATE
     ELSE A.FIXED + B.RATE END APR4

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
or, better even:
LEAST(GREATEST( A.MINRATE , A.FIXED + B. RATE), A.MAXRATE) APR4

Open in new window

0
tigin44Commented:
decode is not suitable for your purpose...
in DECODE you just compare one scaler value with other values... but you want to campore more then one value so continue using case when clause...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CJJCAAuthor Commented:
Thank you,  the LEAST statement that Angel suggested  is working in SQL,   I now have to test it in the online environment where the CASE statment wouldn't provide the expected results.
0
CJJCAAuthor Commented:
Hi,
Although, the Greatest and Least function work in sql, they did not work in the environment I use to place them on forms.
The query was re-wrote so that no logic was involved in the columns being listed.
The query example is below in case the format can be helpful to someone.   Thank you all so much for your help.  I learned how to use a function that I didn't know how to use.  I apprciate your sharing your knowledge.


SELECT
    TO_CHAR(SUB1.APR * 100, '00.00') APRU,
    TO_CHAR(SUB1.APR / DAYS_IN_YEAR * 100, '.0000') DPR9
FROM
    (
    SELECT
       RATE,
        (A. FIXED + B. RATE) DPR1,
        A.MINRATE,
        A.MAXIATE,
        CASE
            WHEN
            (
                A.FIXED + B. RATE
            )
            < A.MINRATE
            THEN A. MINRATE
            WHEN
            (
                A.FIXED + B.RATE
            )
            > A.MAXRATE
            THEN A.MAXRATE
            ELSE (A.FIXED + B.RATE)
        END AS APR,
        DECODE( (SELECT YRNBR FROM CY WHERE YRNBR = (SELECT TO_CHAR(SYSDATE,'YYYY') FROM DUAL)), 'Y', 366, 365 ) as "DAYS_IN_YEAR"
    FROM
        A A ,
        B B
    WHERE
        A.CD = 'CODE'
        AND A.DATE IS NULL
        AND B. NBR = A. DNBR
        AND B.DATE      =
        (
        SELECT
            MAX(DATE)
        FROM
           C C
        WHERE
           C.NBR =  01        )
    )
    SUB1
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.