Greatest Function...

Hi,
I havea query like this:
select trunc(GREATEST(MAX(TRAVEL_START),
                        MAX(TRAVEL_STOP),
                        MAX(LABOR_START),
                        MAX(LABOR_STOP)))
from t1the columns are combinaton such that user can enter travel_start and travel_stop combination OR labor_start and labor_stop combination or both or none of them...
Now,
the query fails when user inputs any one of the combination.. so for eg. user enters labor_start and labor_stop but does not enter Travel start and stop combinaton(i.e. null is stored in travel start/stop) the greatest returns null instead of returning greatest from labor_start/stop combinaton.
What should be done to rectify this..
thx

I missed the requirement that the output should be null when all 4 columns are null, so I added a NULLIF function to achieve the desired result. The example now becomes:
SQL> var TRAVEL_START varchar2(8)
SQL> var TRAVEL_STOP varchar2(8)
SQL> var LABOR_START varchar2(8)
SQL> var LABOR_STOP varchar2(8)
SQL> exec :TRAVEL_START := null;
PL/SQL-procedure is geslaagd.
SQL> exec :TRAVEL_STOP := null;
PL/SQL-procedure is geslaagd.
SQL> exec :LABOR_START := '01012006';
PL/SQL-procedure is geslaagd.
SQL> exec :LABOR_STOP := '31122006';
PL/SQL-procedure is geslaagd.
SQL> select trunc
  2         ( greatest
  3           ( max(to_date(:TRAVEL_START,'ddmmyyyy'))
  4           , max(to_date(:TRAVEL_STOP,'ddmmyyyy'))
  5           , max(to_date(:LABOR_START,'ddmmyyyy'))
  6           , max(to_date(:LABOR_STOP,'ddmmyyyy'))
  7           )
  8         )
  9    from dual
10  /
TRUNC(GREATEST(MAX(
1 rij is geselecteerd.
SQL> select nullif
  2         ( trunc
  3           ( greatest
  4             ( nvl(max(to_date(:TRAVEL_START,'ddmmyyyy')),to_date('1','j'))
  5             , nvl(max(to_date(:TRAVEL_STOP,'ddmmyyyy')),to_date('1','j'))
  6             , nvl(max(to_date(:LABOR_START,'ddmmyyyy')),to_date('1','j'))
  7             , nvl(max(to_date(:LABOR_STOP,'ddmmyyyy')),to_date('1','j'))
  8             )
  9           )
10         , to_date('1','j')
11         )
12    from dual
13  /
NULLIF(TRUNC(GREATE
31-12-2006 00:00:00
1 rij is geselecteerd.
SQL> exec :LABOR_START := null;
PL/SQL-procedure is geslaagd.
SQL> exec :LABOR_STOP := null;
PL/SQL-procedure is geslaagd.
SQL> select nullif
  2         ( trunc
  3           ( greatest
  4             ( nvl(max(to_date(:TRAVEL_START,'ddmmyyyy')),to_date('1','j'))
  5             , nvl(max(to_date(:TRAVEL_STOP,'ddmmyyyy')),to_date('1','j'))
  6             , nvl(max(to_date(:LABOR_START,'ddmmyyyy')),to_date('1','j'))
  7             , nvl(max(to_date(:LABOR_STOP,'ddmmyyyy')),to_date('1','j'))
  8             )
  9           )
10         , to_date('1','j')
11         )
12    from dual
13  /
NULLIF(TRUNC(GREATE
1 rij is geselecteerd.Regards,
Rob.

Similar Messages

  • Leaset() and Greatest function in OBIEE???

    Do Leaset() and Greatest function exists in OBIEE which is present in SQL ??
    if yes what is the syntax for this???

    Hi,
    I think EVALUATE function might help you
    http://gerardnico.com/wiki/dat/obiee/presentation_service/obiee_period_to_period_lag_lead_function
    http://obiee101.blogspot.com/2007/12/obiee-evaluate-function.html
    Regards
    Monika

  • GREATEST Function question

    I have a simple question, but I am not sure about it.
    Say that rank3 has the best score of 99 and it wins, how do I know
    which column it was ? Is there a way to also in addition to bringing
    back the winning rank value, to also bring back which column that came from ?
    select customer_id, GREATEST(rank1, rank2, rank3, rank4, rank5) Winning_Rank
    from table;

    Question has a version dependent answer.
    You need to provide your four digit version always in your request, and you shouldn't need to be asked to provide it.
    My initial reply is it will be difficult due to the non-normalized structure of the table.
    Sybrand Bakker
    Senior Oracle DBA

  • Null handling with greatest function

    Hi , My oracle version is 10.2 and I am trying to get max value from three dates.
    col1,                col2,               col3                                max_date
    *12/4/2007 12/5/2009 7:14:57 PM*
    col1 value is null . so when I use below sql
    select greatest(NVL(col1,col2)) from temp_greatest where rec_id=366957734, I got value 12/4/2007
    I want to get max date from col1,col2 and col3, even though col1 is null.
    I have couple of rows with col2 acnd col3 as null values.
    How to get the max values.. any help appreciate
    Thanks

    Here you go.
    CREATE TABLE SSPTRIG.TEMP_GREATEST_1207
    COl1_DATE DATE,
    col2_DATE DATE,
    col3_DATE DATE,
    max_date DATE
    SET DEFINE OFF;
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (NULL, TO_DATE('12/04/2007 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('12/05/2009 19:14:57', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/03/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 05:26:08', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/03/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 05:26:07', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/04/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 06:10:55', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('08/09/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('12/05/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('12/05/2009 21:01:12', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/04/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 05:44:37', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/04/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 05:44:33', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/04/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 06:41:14', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('12/04/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 05:44:31', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TEMP_GREATEST_1207
    (COL1_DATE,COL2_DATE,COL3_DATE)
    Values
    (TO_DATE('10/08/2008 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NULL, TO_DATE('12/05/2009 21:02:38', 'MM/DD/YYYY HH24:MI:SS'));
    COMMIT;
    i NEED TO POPULATE MAX DATE INTO MAX_DATE COLUMN IN TEMP_GRATEST_1207.
    THANKS.

  • SIMPLE question about GREATEST FUNCTION

    select greatest('2005-1-1', '2004-1-1') from dual will return 2005-1-1
    select greatest('2005-1-1', '2004-1-1', '') from dual will return nothing, I want it to return 2005-1-1
    Any work around?

    First thing I tried was to clarify in my head that the last option had a space.
    so I tried the following...
    select greatest('2005-1-1', '2004-1-1') from dual got 2005-1-1
    select greatest('2005-1-1', '2004-1-1', '') from dual got BLANK
    select greatest('2005-1-1', '2004-1-1', ' ') from dual got 2005-1-1....
    So you need to put the space in between the ticks.

  • Function to find the greatest of two numbers

    Hi All
    In the rtf template, is there any function to find the greatest between two numbers?
    Please help
    Thanks in Advance

    Just like SQL, use the GREATEST function. List of comparable SQL functions is shown on or about page 9-30 of user guide (depends on which version you have). Search on GREATEST and you'll find it.

  • Using case when to an aggregate function

    Hi,
    I have a sql statement like below,
    Select CASE WHEN (Sum(Amount) Over (Partition By Name),1,1) = '-' THEN 0 ELSE Sum(Amount) Over (Partition By Name) END AS Amount_Person
    From tbPerson
    But when I run the sql statement above I got error ORA-00920: invalid relational operator. What I'm trying to do is when the total amount for each person is negative then it will return 0 else it will return the positive value. I dont want to use the GROUP BY function. Is there any other way than using the Sum Over function? Thanks

    Like this?
    SELECT CASE WHEN Sum(Amount) Over (Partition By Name) < 0 THEN 0
                ELSE Sum(Amount) Over (Partition By Name)
           END AS Amount_Person
    FROM tbPerson
    ;or using GREATEST function :
    SELECT GREATEST(
             Sum(Amount) Over (Partition By Name)
           , 0
           ) as Amount_Person
    FROM tbPerson
    ;Edited by: odie_63 on 24 févr. 2011 09:12

  • Sql greatest,decode,instr.  minimize code lines sql

    I did not write this code and please excuse me for my
    beginner questions?
    · Is there a way to make this code cleaner. Code it
    where its less lines of code?
    · Can we make the 2 conditions separate rather than a
    one after the other?
    For ex. Not like this WHEN
    INSTR(CASESTAB_1.CASENOTXT,'-ll’) > 0 THEN
    decode(CASESTAB_1.RECDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),CASESTAB_1.RECDT)
    · why is it in a pair like this:
    WHEN INSTR(CASESTAB_1.CASENOTXT,'-ll’) > 0 THEN
    decode(CASESTAB_1.RECDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),CASESTAB_1.RECDT)
    if the string to find in
    CASESTAB_1.CASENOTXT is independent to comparing date?
    Can the string search and date be separate?
    · Is the a shorter way of comparing the 4 dates.?
    Alternatives
    Greatest(
    CASE
    WHEN INSTR(CASESTAB_1.CASENOTXT,'-ll’) > 0 THEN
    decode(CASESTAB_1.RECDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),CASESTAB_1.RECDT)
    WHEN INSTR(CASESTAB_1.CASENOTXT,'-ss’) > 0 THEN
    decode(ERTAB.FINERDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),ERTAB.FINERDT)
    WHEN INSTR(CASESTAB_1.CASENOTXT,'-dd ') > 0 THEN
    decode(ERTAB.PUBLICATIONDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),ERTAB.PUBLICATIONDT)
    WHEN INSTR(CASESTAB_1.CASENOTXT,'-ff’) > 0 THEN
    decode(ERTAB.PUBLICATIONDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),ERTAB.PUBLICATIONDT)
    ELSE
    decode(CASESTAB.CPDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),CASESTAB.CPDT)
    END
    ,decode(CASESTAB.CPDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),CASESTAB.CPDT))
    sql finds the suffixes from CASESTAB_1.CASENOTXT
    AND
    compares dates 4
    · CASESTAB_1.RECDT
    · ERTAB.FINERDT
    · ERTAB.PUBLICATIONDT
    · CASESTAB.CPDT
    AND GETS THE LATER OF THE 4 DATES.
    1. \find the suffix from caseN0 field/col
    -ll
    --ss
    -dd
    -ff
    CASESTAB_1.CASENOTXT
    2. dates to find the latest date.
    · CASESTAB_1.RECDT
    · ERTAB.FINERDT
    · ERTAB.PUBLICATIONDT
    · CASESTAB.CPDT
    Using greatest function.
    3. the tables are
    1. ERTAB
    2. CASESTAB
    3. CASESTAB

    Well, I don't know how much simpler it could get and still do
    what it is designed to do. For example, the statement
    WHEN INSTR(CASESTAB_1.CASENOTXT,'-ll’) > 0 THEN
    decode(CASESTAB_1.RECDT,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),CASESTAB_1.RECDT)
    basically means that if the CASENOTXT column contains
    '-ll’, then look at the RECDT column and if it is NULL, use
    the date value of '01/01/1900', otherwise use the date value in the
    RECDT column.....
    and so on through the other WHEN clauses in your CASE
    statement. Is this what you are expecting thsi query to do?
    Phil

  • Greatest and Max in sql

    greatest means it take the high value frm the exprn,,,,isnt it?
    so in this eg: SELECT greatest('22', '5', '60', '1000','1') FROM DUAL;----------Ans is : *60*
    select GREATEST('aby', 'john', 'faisal') from dual; --------Ans is john
    how it get these outputs outut,,pls help me
    and how to find the greatest or higest date from one table using greatest function,
    pls explain with an example
    thnz
    Edited by: 821553 on May 17, 2011 3:16 PM
    Edited by: 821553 on May 17, 2011 3:23 PM

    821553 wrote:
    bt i didnt understand yet that how it get the answer '60'
    select GREATEST('aby', 'john', 'faisal') from dual; --------Ans is john
    how it get these outputs?/how it compare the two charctersIf you look at how the data is stored internally...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 'aby' as nm from dual union all
      2             select 'john' from dual union all
      3             select 'faisal' from dual)
      4  --
      5  -- end of test data
      6  --
      7  select nm, dump(nm) as dump_nm
      8* from t
    SQL> /
    NM     DUMP_NM
    aby    Typ=1 Len=3: 97,98,121
    john   Typ=1 Len=4: 106,111,104,110
    faisal Typ=1 Len=6: 102,97,105,115,97,108
    SQL>You can see that the bytes representing each characters have numeric values, and it each of these that are compared from left to right. 'john' starts with a 'j' which is 106 and that's greater than 97 or 102, so john is the greatest.
    If there were two names beginning with 'j' then the next character is compared...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 'aby' as nm from dual union all
      2             select 'john' from dual union all
      3             select 'jon' from dual union all
      4             select 'faisal' from dual)
      5  --
      6  -- end of test data
      7  --
      8  select nm, dump(nm) as dump_nm
      9* from t
    SQL> /
    NM     DUMP_NM
    aby    Typ=1 Len=3: 97,98,121
    john   Typ=1 Len=4: 106,111,104,110
    jon    Typ=1 Len=3: 106,111,110
    faisal Typ=1 Len=6: 102,97,105,115,97,108In this example we have two names beginning "j" which is the greatest of the first character, so the second character is looked at, and there is still two names because they both have the character "o", so it's the 3rd character that is looked at with "h" (104) being compared to "n" (110) to determine that "jon" is the greatest....
    SQL> select greatest('aby','john','jon','faisal') from dual;
    GRE
    jon
    SQL>

  • Combine greatest, sum, and case when

    All,
    I have the following view
    Select table1.field1,
    greatest(sum(table1.field3) - case when table1.oneorzerocolumn=1 then table1.field2 else table1.field4 end,0) as NetAbove0
    from table1
    group by table1.field1;
    I seem to be having trouble figuring out the best way to accomplish this, though, as whenever I try to execute this query I get not a valid group by expression. How can I combine the greatest function and the aggregate sum function without creating a separate view?
    Thanks in advance.
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    CORE     11.2.0.2.0     Production
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production
    Edited by: 929933 on Mar 14, 2013 1:32 PM

    929933 wrote:
    @ sb92075 would you care to elaborate on exactly what was missing?If you would care to read the link sb92075 posted you will understand by yourself:
    What is particularly important in that FAQ is:
    6) Tables/Indexes
    7) Sample Data
    8) Expected Output
    9) Formatting with {noformat}{noformat} Tags
    You have posted an unformatted code and did not provide proper input.
    Coming to your problem the issue is quite clear. If you use a GROUP BY clause then all field which are not returned as aggregated function shall be included in the GROUP BY clause.
    And this has a logic.
    Your initial query (formatted in a decent way):SELECT table1.field1
    , GREATEST (SUM (table1.field3)
    - CASE
    WHEN table1.oneorzerocolumn = 1 THEN
    table1.field2
    ELSE table1.field4
    END, 0) AS netabove0
    FROM table1
    GROUP BY table1.field1;
    is not correct.
    The column used in the query table1.oneorzerocolumn, table1.field2 and table1.field4 are not part of the GROUP BY and not used in aggregation functions.
    So how could you determine the value, i.e. table1.oneorzerocolumn, if for the same value of table1.field1 you have different values of table1.oneorzerocolumn?
    You are aggregating by field1 so which value do you want to use for non aggregated columns?
    Everything will be easier to understand if you can post some sample data, explain the logic you want to apply and post the expected output for the sample data you have posted.
    Regards.
    Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Problems with GREATEST() = Bug?

    I'm working on a view and got a problem with the GREATEST()-function - I'm using 11.2.
    Below I#ve past some code with comments which should explain the problem - but you're welcome to ask any more questions ;)
    -- first executeing the query separated to check it return valus   2.
    (SELECT nvl(max(acqtime),to_timestamp('01-01-1971','dd-mm-yyyy')) FROM rfm_meas_hr WHERE box_id IN (SELECT...WHERE id=4) );   
    -- returns: 01.01.71 00:00:00,000000000
    (SELECT nvl(max(acqtime),to_timestamp('01-01-1971','dd-mm-yyyy')) FROM rfm_meas_10m WHERE box_id IN (SELECT...WHERE id=4) );
    -- returns: 11.08.10 16:00:00,000000000
    -- therefore i assume if i use GREATEST() with this both timestamps ist would return the second one
    SELECT GREATEST(
            to_timestamp('01-01-1971 00:00:00,000','dd-mm-yyyy hh24:mi:ss,ff'),
            to_timestamp('11-08-2010 16:00:00,000','dd-mm-yyyy hh24:mi:ss,ff')
    ) FROM dual;
    -- do that job like expected
    -- BUT
    SELECT GREATEST(
            (SELECT nvl(max(acqtime),to_timestamp('01-01-1971','dd-mm-yyyy')) FROM rfm_meas_hr WHERE box_id IN (SELECT...WHERE id=4) ),
            (SELECT nvl(max(acqtime),to_timestamp('01-01-1971','dd-mm-yyyy')) FROM rfm_meas_10m WHERE box_id IN (SELECT...WHERE id=4) )
    ) FROM dual;
    -- returns the 1971 timestamp wich is the wrong result!
    -- any ideas?alternativly you can view the code on pastebin: http://pastebin.com/uGVDUMuU
    I want to know if this behaviour of GREATEST() is on purpose or if I did some mistake or anything else!?
    Thanks!

    As you read my examples above I worked with two tables (rfm_meas_hr and rfm_meas_10m). In a job i take the timestamp from rfm_meas_hr and put it into the rfm_meas_10m table. And at this point the full year information gets lost!
    Code example from that job:
    FOR meas IN
                SELECT
                    to_timestamp(MIN(acqtime),'dd-mm-yyyy hh24:mi:ss,ff3') AS acqtime,
                FROM
                    rfm_meas_hr
                WHERE
                GROUP BY
                ORDER BY
                    acqtime
    LOOP
                -- each result row containing summarized data is accesible on "meas"
                INSERT INTO rfm_meas_10m VALUES meas;
    END LOOP;The acqtime which is inserted to rfm_meas_10m contains the lost year-information, but in the acqtime-column of rfm_meas_hr are the full information avaiable. I've tested this with:
    SELECT to_char(acqtime,'dd-mm-yyyy hh24:mi:ss,ff') FROM rfm_meas_hr;
    -- and compare it to
    SELECT to_char(acqtime,'dd-mm-yyyy hh24:mi:ss,ff') FROM rfm_meas_10m;So may be it worth a try to leave out the to_timestamp-call in the select inside of the job code?! .. I'm going to try.
    [edit]
    @ user503699      
    No .. it produces a year like: 0010 ;) But as we see the error might not be a problem with LEAST/GREATEST anymore.
    Edited by: Stede on 12.08.2010 05:24

  • HOW CAN I CHANGE THE CODE IN MY DECODE?

    Is it possible to do a DECODE where a number or phrase could be returned
    depending upon the condition?
    Currently I have this DECODE in my SELECT STATEMENT
    select
    DECODE(
    GREATEST( MONTHS_BETWEEN(n.lcl_er_recv_dt,
    ADD_MONTHS(n.ind_er_per_end_dt,3)) , 0
    ,0,NULL,
    DECODE(
    LEAST( MONTHS_BETWEEN(n.lcl_er_recv_dt,
    ADD_MONTHS(n.ind_er_per_end_dt,3)) , 3
    ,3, 'OVER 3',
    TO_CHAR(ROUND(MONTHS_BETWEEN(n.lcl_er_recv_dt,
    ADD_MONTHS(n.ind_er_per_end_dt,3)))
    ) late
    from ncoer n,
    all_cmd_view acv
    where lcl_er_form_cd = '4'
    and n.psc_cd not in ('SC01')
    and n.PSC_CD = 'UA72'
    and n.lcl_er_recv_dt >= '01-MAR-2007'
    and n.lcl_er_recv_dt <= '31-MAR-2007'
    and n.lcl_er_recv_dt > ADD_MONTHS(ind_er_per_end_dt,3)
    and nvl(N.lcl_omit_from_stats_ind,'N') <> 'Y'
    and not exists ( select 1
    from ncoer_in_error
    where ind_ssn = n.ind_ssn
    and ind_er_per_end_dt = n.ind_er_per_end_dt
    and lcl_er_recv_dt = n.lcl_er_recv_dt)
    and acv.cmd_cd = n.mil_cmd_asgn_cd
    and NVL(acv.lcl_code_stat,'N') = 'Y'
    I have to use the GREATEST FUNCTION to return a value of null if I get a number
    less than 0. I have to use the LEAST FUNCTION to return the value 'OVER 3'
    when the calculated value is greater than 3.
    Could I change it so if the result of the DECODE equals '0' months the phrase
    '90 +DAYs'  is returned
    or if the result of the DECODE equals '1' month then a '1' is returned
    or if the result of the DECODE equals '2' months then a '2' is returned
    or if the result of the DECODE equals '3' months then a '3' is returned
    or if the result of the DECODE > '3' then a 'OVER 3' is returned.

    It never hurts to try it out.
    WITH t AS
    SELECT 0 col1 FROM DUAL UNION ALL
    SELECT 1 FROM DUAL UNION ALL
    SELECT 2 FROM DUAL UNION ALL
    SELECT 3 FROM DUAL UNION ALL
    SELECT 4 FROM DUAL)
    SELECT
      col1                    col1
      ,DECODE(col1
              ,0, '90 +DAYs'
              ,1, '1'
              ,2, '2'
              ,3, '3'
              ,'OVER 3')      test_decode
    FROM
      t;
    COL1                   TEST_DECODE
    0                      90 +DAYs   
    1                      1          
    2                      2          
    3                      3          
    4                      OVER 3     
    5 rows selected

  • How to find max value in single row

    i would like to know that,
    how can i display maximum value among several values in a single row of table
    example : i have to get value 5
    among 1 2 3 4 5 values of same row.

    Take a look at GREATEST function.
    C.
    Link to documentation added
    Message was edited by:
    cd

  • PL/SQL help in Package

    Hi all,
    I have a block of code in a package that runs 3 times to spit out a Total.
    I want to capture the 3 Totals and return the highest total..
    so whats the best way to store those three values and select the highest Total from the same package....
    Edited by: 874167 on Jul 26, 2011 2:35 PM

    874167 wrote:
    I want to capture the 3 Totals and return the highest total..
    so whats the best way to store those three values and select the highest Total from the same package....Packages allow static variables to be created. The values can be stored as either private (defined in the package body/implementation section) or global (defined in package header/interface section) static variables.
    The package function that returns the highest number can use the Greatest() function. E.g.
    SQL> declare
      2     n       number;
      3  begin
      4     n := Greatest( 100, 123, 92, 81, 156 );
      5     dbms_output.put_line( 'max='||n );
      6* end;
    SQL> /
    max=156
    PL/SQL procedure successfully completed.
    SQL>

  • Query format

    I HAVE A TABLE LIKE THIS
    No Name C1 C2 C3
    1 ABC 10 20 50
    2 DEF 30 60 30
    3 EFG 90 80 10
    4 IJK 60 20 99
    AND I NEED OUTPUT AS LIKE - PUT 'Y' WHERE THE GREATEST VALUE IN A ROW
    AND ELSE 'N'.
    AS.......
    NAME C1 C2 C3
    ABC N N Y
    DEF N Y N
    EFG Y N N
    IJK N N Y
    THE OUTPUT MUST BE ONLY THROUGH A QUERY NOT USING A BLOCK ...... IT POSSIBLE THROUGH NESTING THE DECODE FUNCTION BUT I DONT KNOW HOW .........
    PLEASE HELP ME................

    Use GREATEST Function and DECODE.

Maybe you are looking for