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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Similar Messages

  • A sum and case query help

    I need help please with the following query.
    I have a table that contains a currency, voucher, start_date and a value.
    Data in the tables looks like this:
    GB, 31/05/2010, A, 100
    GB, 31/05/2010, B, 250
    GB, 31/05/2010, A, 72
    GB, 12/12/2009, A, 1000
    GB, 12/12/2009, B, 72
    LX, 12/05/2010, A, 90
    This is where it gets complicated, I need to show the total(SUM) value when the start_date < 60 days from sysdate and also when the start_date > 61 days AND only if the voucher = A and partioned by the currency.
    So, in other words I need my results like this.
    GB, 31/05/2010, A, 100, *172, 0*
    GB, 31/05/2010, B, 250, *0, 0*
    GB, 31/05/2010, A, 72, *172, 0*
    GB, 12/12/2009, A, 1000, *0, 1000*
    GB, 12/12/2009, B, 72, *0, 0*
    LX, 12/05/2010, A, 90, *90, 0*
    The bold columns are what I need, one called less_than and one more_than.
    A big big thank you and any advice I appreciate.
    S

    Please make a habit of posting sample data we can work with right away by using CREATE TABLE and INSERT INTO statements, or use the WITH clause as shown below:
    SQL> with t as ( -- generating sample data:
      2  select 'GB' currency, to_date('31/05/2010', 'dd/mm/yyyy') start_date, 'A' voucher, 100 value from dual union
      3  select 'GB', to_date('31/05/2010', 'dd/mm/yyyy'), 'B', 250 from dual union
      4  select 'GB', to_date('31/05/2010', 'dd/mm/yyyy'), 'A', 72 from dual union
      5  select 'GB', to_date('12/12/2009', 'dd/mm/yyyy'), 'A', 1000 from dual union
      6  select 'GB', to_date('12/12/2009', 'dd/mm/yyyy'), 'B', 72 from dual union
      7  select 'LX', to_date('12/05/2010', 'dd/mm/yyyy'), 'A', 90 from dual
      8  )
      9  --
    10  -- actual query:
    11  --
    12  select currency
    13  ,      start_date
    14  ,      voucher
    15  ,      value
    16  ,      sum(case
    17               when  trunc(sysdate-start_date)< 60
    18               and  voucher = 'A'
    19               then value
    20               else 0
    21             end) over (partition by currency, voucher, start_date) sum_val1
    22  ,      sum(case
    23               when trunc(sysdate-start_date)> 61
    24               and  voucher = 'A'
    25               then value
    26               else 0
    27             end) over (partition by currency, voucher, start_date) sum_val2
    28  from   t;
    CU START_DAT V      VALUE   SUM_VAL1   SUM_VAL2
    GB 12-DEC-09 A       1000          0       1000
    GB 31-MAY-10 A         72        172          0
    GB 31-MAY-10 A        100        172          0
    GB 12-DEC-09 B         72          0          0
    GB 31-MAY-10 B        250          0          0
    LX 12-MAY-10 A         90         90          0
    6 rows selected.

  • Subquery  and case when

    hi
    i have the following script
    if run the subquery individually it rgives me result
    but when try to run the whiole query i get error
    at line as from kyword not found where expected
    the query is
    SELECT
    Q2.BRANCH,
    Q2.NAMETITLE,
    Q2.PRD,
    Q2.LONGNAME,
    Q2.BALANCE1,
    Q2.BALANCE2,
    Q2.BALANCE3,
    Q2.BALANCE4,
    Q2.BALANCE5,
    Q1.REMARK
    FROM
    SELECT
    PRDACCTID PRD,
    LBRCODE BRANCH,
    case
    when
         (prdacctid,lbrcode) in 
              select prdacctid,lbrcode from d010053
                     where (lbrcode,prdacctid)
              in (
                   select lbrcode,prdacctid from d009022 where acctstat<>3
                   and (lbrcode,trim(substr(prdacctid,1,8))) in
                        (select lbrcode,trim(prdcd) from d009021 where moduletype in (10,11,12))
         and nametype=1
    then
         'yes'
    else
         'no'
    end
    ) as remark
    FROM D009022
    WHERE ACTSTAT<>3
    AND TRIM(SUBSTR(PRDACCTID,1,8)) IN ('SB','CD','CC')
       )Q1,
              SELECT
              BRANCH,
              NAMETITLE,
              PRD,
              LONGNAME,
              BALANCE1,
              BALANCE2,
              BALANCE3,
              BALANCE4,
              BALANCE5,
              FROM
                        SELECT A.*,B.* ,
                        A.PRDACCTID PRD,
                        A.LBRCODE BRANCH,
                        RANK() OVER(PARTITION BY B.LBRCODE,B.PRDACCTID ORDER BY B.CBLDATE DESC) AS RNK
                        FROM D009022 A,D010014 B
                        WHERE A.LBRCODE=B.LBRCODE
                        AND A.PRDACCTID=B.PRDACCTID
                        AND CBLDATE<'01-APR-09'
                        AND A.ACCTSTAT!=3
                        AND (TRIM(SUBSTR(A.PRDACCTID,1,8)),A.LBRCODE) IN
                        (SELECT TRIM(PRDCD),LBRCODE FROM D009021 WHERE MODULETYPE IN (10,11,12,13,14,30))
                   WHERE RNK=1
       )Q2
    WHERE Q1.BRANCH=Q2.BRANCH
    AND Q1.PRD=Q2.PRD
    /

    If you formatted your query better, then you might have spotted the issue yourself...
    In your Q2 query, you've got "BALANCE5 *,* FROM ..." so remove the extra comma, like so:
    SELECT Q2.BRANCH,
           Q2.NAMETITLE,
           Q2.PRD,
           Q2.LONGNAME,
           Q2.BALANCE1,
           Q2.BALANCE2,
           Q2.BALANCE3,
           Q2.BALANCE4,
           Q2.BALANCE5,
           Q1.REMARK
    FROM  (SELECT PRDACCTID PRD,
                  LBRCODE BRANCH,
                  case when (prdacctid,lbrcode) in (select prdacctid,lbrcode
                                                    from   d010053
                                                    where  (lbrcode,prdacctid) in (select lbrcode,prdacctid
                                                                                   from   d009022
                                                                                   where  acctstat != 3
                                                                                   and    (lbrcode,trim(substr(prdacctid,1,8))) in (select lbrcode,trim(prdcd)
                                                                                                                                    from   d009021
                                                                                                                                    where  moduletype in (10,11,12)))
                                                    and    nametype=1)
                             then 'yes'
                       else 'no'
                  end as remark
           FROM   D009022
           WHERE  ACTSTAT != 3
           AND    TRIM(SUBSTR(PRDACCTID,1,8)) IN ('SB','CD','CC')) Q1,
          (SELECT BRANCH,
                  NAMETITLE,
                  PRD,
                  LONGNAME,
                  BALANCE1,
                  BALANCE2,
                  BALANCE3,
                  BALANCE4,
                  BALANCE5    -- removed extra comma from here
           FROM   (SELECT A.*,B.* ,
                          A.PRDACCTID PRD,
                          A.LBRCODE BRANCH,
                          RANK() OVER(PARTITION BY B.LBRCODE,B.PRDACCTID ORDER BY B.CBLDATE DESC) AS RNK
                   FROM   D009022 A,D010014 B
                   WHERE  A.LBRCODE=B.LBRCODE
                   AND    A.PRDACCTID=B.PRDACCTID
                   AND    CBLDATE < to_date('01/04/2009', 'dd/mm/yyyy')
                   AND    A.ACCTSTAT != 3
                   AND    (TRIM(SUBSTR(A.PRDACCTID,1,8)),A.LBRCODE) IN (SELECT TRIM(PRDCD),LBRCODE
                                                                        FROM   D009021
                                                                        WHERE  MODULETYPE IN (10,11,12,13,14,30)))
           WHERE  RNK=1) Q2
    WHERE  Q1.BRANCH = Q2.BRANCH
    AND    Q1.PRD = Q2.PRDEdited by: Boneist on 06-Aug-2009 10:20
    Added in the to_date around the date which I forgot to point out earlier (but BluShadow has already done so!)
    Also added in a comment on the line I removed the extra comma from.

  • Pro C syntax and CASE WHEN

    Hi
    I'm developping an application using Pro C* in a C program. I have a problem, I would like to use a CASE WHEN statement inside a SELECT statement. I try the following code with compilation error:
    EXEC SQL PREPARE pr_avor_annul FROM
    INSERT INTO avor (NO_AVOR,MT_TOTL_AVOR,CD_DEVS,AC_STAT_AVOR,NO_FACT,MT_FRAS,
    MT_ARTC,AC_AVOR,NO_COMP,MT_FRAS_CONT_REMB,DT_AVOR)
    SELECT :vavor, mt_totl_fact + cout_livr + nvl(mt_fras_cont_remb,0), cd_devs,:vemis ,no_fact,cout_livr , mt_totl_fact, :vacavor, :vcomp, mt_fras_cont_remb,DECODE( (SELECT 0 FROM fact WHERE TO_CHAR(dt_fact)<TO_CHAR(sysdate)
    AND no_fact= :vfact),dt_fact, sysdate, dt_fact)
    FROM fact
    WHERE no_fact = :vfact;
    This code seems to be wrong but I don't know where...
    Somebody can help me??
    Thans a lot

    Hi,
    I'm not clear on the distinction you're making for type of access.
    Well, to be honest, now that I've re-read that this morning I'm not completely sure what I had in mind either. I think I was thinking about using the collection in what I believe the documentation calls "slices". However, given the other information you've posted it doesn't seem like that would be a feasible alternative in any case.
    I understand what you mean about varray and nested-table with the upper-bound limit for the varray's... I've not actually compared the two for performance so my idea that there may be a difference could be completely incorrect. Beyond that, creating code that only works in batches (or slices) may be less performant than what you currently have.
    I hope I've not detracted from the thread and maybe someone else with more experience will have an "ah ha!" sort of observation.
    Regards,
    Mark

  • Skype doesn't combine pinned icon and taskbar when...

    This was already reported but nobody from skype replied check here
    I have this issue on my Surface Pro 3, with win 8.1 Once i lunch skype it open a taskbar, but still keep skype pinned icon separated... it make me loose space. Worked perfectly before on Windows 7. Version of skype 7.5.64.101
    Attachments:
    Untitled.jpg ‏20 KB

    Hi Lax561,
    Welcome to discussions ...
    I can see that Safari is NOT in the Dock ...
    Did you check out if Safari is still in the Application folder ?? open Finder an go to the application folder.
    If it's still there than you can drag a copy to the Dock.
    Do the following ... Select Safari icon an hold down the mouse an the Option key ad the same time while dragging to the Dock. During dragging you will see a + (plus) next to the Safari icon.
    Place the Safari icon in the Dock where you like it, next to the Fire Fox icon is maybe the best place to put it.
    Post back with the results ...
    Dimaxum

  • How to use case when statements in ODI

    I need to put conditional logic before DVM look up in ODI. In the expression editor I put the following statement:-
    CASE WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS' THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=EBIZ_CELL.CELL_DATA
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC' THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=EBIZ_CELL.CELL_DATA
    END
    It did not work,
    Under Operators ->All Executions, found the error:-
    905 : 42000 : java.sql.SQLException: ORA-00905: missing keyword
    The description in Session Task contained:-
    select     
         C1_JOURNAL_TEMPL
    from     APPS.POC_JOURNAL_TEMP_SOURCE_TBL POC_JOURNAL_TEMP_SOURCE_TBL, APPS.C$_0POC_JOURNAL_TEMP_TARGET_TB
    where     
         (1=1)
    And (CASE
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
    THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=C2_CELL_DATA
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
    THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=C2_CELL_DATA
    END)
    Checked the above code in PL/SQL Developer but it gave errors.
    In PL/SQL developer tried to check a simple query using case-when but even that is giving errors in the case-when portion.
    The query is as follows:-
    select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD
    from prj_detail
    where
    case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    when 'EXT'
    then
    1
    when 'RAC'
    then
    2
    when 'XXX'
    then
    3
    else
    end
    I would like to know what is wrong with the above code.
    Please let me know what is the correct way of using case-when in PL/SQL as well as in ODI.

    Your ODI case statement and PL/SQL Case statement both looks confusing.
    You are writing case statement under where clause which is not a good practise. If you want to implement logic like-
    select a,b,c from <table>
    where
    when cond^n^ 1 then do 1
    when cond^n^ 2 then do 2
    then better you seperate your query for each filter and do a union, in other words-
    select a,b,c from <table>
    where cond^n^ 1
    union
    select a,b,c from <table>
    where cond^n^ 2
    If you are writing case staement to retrieve a value/column (EBIZ_CELL.CELL_DATA) then no need to include it under filter.
    ODI case for column EBIZ_CELL.CELL_DATA will be:
    CASE
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
    THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
    THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)
    END
    Pl/SQL query-
    select phase_code, accounting_period,
    case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    when 'EXT'then 1
    when 'RAC' then 2
    when 'XXX' then 3
    else 'default value' --- (if no else needed then can also remove else part)
    end,
    sum(eff_cc) as BD_Eff_QTD
    from prj_detail
    Suggested as per what is understood, hope it helps.
    Edited by: 939451 on Jul 5, 2012 12:47 AM
    Edited by: 939451 on Jul 5, 2012 12:48 AM

  • Returne between dates in case when

    i want to return between dates values on case when clauses on where clauses
    like
    and
    (case
    when (cc.segment3 NOT like '4%' and cc.segment3 NOT like '5%')
    then (between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr'))
    when (cc.segment3 like '4%' or cc.segment3 like '5%')
    then (between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr'))
    end) h.default_effective_date
    the problem is in the = operator but i don't know haw to use this
    any help??

    ok here is the full coad
    select
    DECODE(SUBSTR (CC.segment1, 1, 1), 'J', 'OFFSHORE', 'I', 'OFFSHORE','IN COUNTRY')branch_nature,
    decode(cc.segment1,'A6260','Head Office','Branches') branch_type,
    cc.segment1,
    decode (h.currency_code,'EGP','mahly','agnaby') currency_code ,
    cc.segment3,
    t.description,
    CASE
    when h.currency_code in('EGP') then sum(l.entered_cr)
    else 0
    end entered_cr,--sum(l.entered_cr) entered_cr,
    CASE
    when h.currency_code in('EGP') then sum(l.entered_dr)
    else 0
    end entered_dr, --sum(l.entered_dr) entered_dr,
    CASE
    when h.currency_code NOT in('EGP') then sum(l.accounted_cr)
    else 0
    end accounted_Dr,--0 accounted_Dr,
    CASE
    when h.currency_code NOT in('EGP') then sum(l.accounted_dr)
    else 0
    end accounted_cr --0 accounted_cr
    from apps.gl_je_headers h,
    apps.gl_je_lines L,
    apps.gl_code_combinations cc,
    apps.fnd_flex_values_tl t ,
    applsys.fnd_user us,
    apps.gl_je_batches b
    where h.status = 'P'
    and us.user_id = h.Created_by
    --and h.currency_code in('EGP')
    and l.je_header_id = h.je_header_id
    and l.code_combination_id =cc.code_combination_id
    and cc.segment3 = t.flex_value_meaning
    and cc.segment3 in ('31000020','40505020')
    and cc.segment1 in ('A5550','B0010')
    -- and (DECODE(SUBSTR (CC.segment1, 1, 1), 'J', 'OFFSHORE', 'I', 'OFFSHORE','IN COUNTRY') =:P_branch_nature OR :P_branch_nature is NULL)
    --and (decode(cc.segment1,'A6260','Head Office','Branches') = :P_BRANCH_TYPE OR :P_BRANCH_TYPE is NULL)
    and t.description is not null
    and t.language ='AR'
    and h.je_batch_id = b.je_batch_id
    and
    (case
    when (cc.segment3 NOT like '4%' and cc.segment3 NOT like '5%')
    then (between to_date(:P_Start_date,'dd/mm/rrrr') and to_date(:P_END_date,'dd/mm/rrrr'))
    when (cc.segment3 like '4%' or cc.segment3 like '5%')
    then (between to_date(:P_Start_date2,'dd/mm/rrrr') and to_date(:P_END_date,'dd/mm/rrrr'))
    end) h.default_effective_date
    and (case
    when (cc.segment3 like '4%' or cc.segment3 like '5%')
    then (h.default_effective_date)
    end) between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr')*/
    group by cc.segment3 , t.description , cc.segment1,h.currency_code;
    the part between tages need to modigied

  • Using more than one case when statement

    Hi there,
    I have a question on using case when statements.
    Currently I have a table where it shows me mulitple dates.
    Order Saledate TransferDate StartDate Enddate GetDate
    I have created a where statement to show me records against the transferdate dependant if it appears within the date range of the year, or if it appears within the startdate and today's date.
    (lpatran.trandate between targets.startdate
    and (case when targets.enddate < getdate() then targets.enddate else getdate() end))
    However on some of the records the transfer date is null, and so they want to use the saledate as the date to use.
    So I created
    (case when Lpatran.trandate is not null then (Lpatran.trandate between targets.startdate
    and (case when targets.enddate < getdate() then targets.enddate else getdate()))
    else (lpatran.saledate between targets.startdate
    and (case when targets.enddate < getdate() then targets.enddate else getdate())) end )
    However it gives me an incorrect syntax near the word ‘between ‘ 
    Is there a simple fix for this or does sql not allow me to do this?

    I have created a new column called transferdate and done a case when statement
    CASE
    WHEN lpatran.trandate
    IS
    NULL
    THEN lpatran.saledate
    ELSE lpatran.trandate
    END
    AS Transferdate,
    Then created a temptable and did a select * from temptable.  Created a where statement from transferdate, which does work and gives me the correct data. 
    TransferDate between startdate
    and(case
    when enddate
    <
    getdate()
    then enddate
    else
    getdate()
    end))
    However I wanted to know if there was a way of obtaining the same data without having to use the temptable ?

  • Error in case when

    hi friends
        i have below query which is throwing error  'missing expression' which is on bold.
        please correct the query.
       SELECT max(lv.lv_lastupdate)  INTO v_old_date FROM loanable_value lv
       WHERE lv.lv_rm_cust_id= p_gfcid AND lv.lv_year= p_year
       AND  (CASE  WHEN     p_Quarter ='Q1'  THEN    lv.lv_month IN ('Jan','Feb','Mar')  END ) ;

    RajnishChauhan wrote:
    hi friends
        i have below query which is throwing error  'missing expression' which is on bold.
        please correct the query.
       SELECT max(lv.lv_lastupdate)  INTO v_old_date FROM loanable_value lv
       WHERE lv.lv_rm_cust_id= p_gfcid AND lv.lv_year= p_year
       AND  (CASE  WHEN     p_Quarter ='Q1'  THEN    lv.lv_month IN ('Jan','Feb','Mar')  END ) ;
    How do you expect us to correct it if we don't know what you want?
    However, I'm guessing you want
    and (
    (p_quarter = 'Q1' and lv.lv_month in ('Jan','Feb','Mar'))
    or (p_quarter = 'Q2' and lv.lv_month in ('Apr','May','Jul'))
    or..
    difficulties you could have avoided if you'd not split things up as lv_year,lv_month etc. and just kept the field as a single DATE.
    Then it would be a simple as
    where to_char(lv.lv_date,'Q')=p_quarter
    where p_quarter is 1,2,3 or 4

  • Difference of sql query in SQL Workshop and SQL Report (case when sum(...

    Hi ,
    I need some help, pls.
    The follwg report runs in SQL Workshop - but gives errors in the sql-query report in HTMLDB:
    SQL-Workshop:
    select
    case
    when SUM(S.POTENTIAL_GROWTH)> 1 and SUM(S.POTENTIAL_GROWTH) < 4 then 'Ok'
    when SUM(S.POTENTIAL_GROWTH)> 4 and SUM(S.POTENTIAL_GROWTH) < 8 then 'SoSo'
    when SUM(S.POTENTIAL_GROWTH)> 8 and SUM(S.POTENTIAL_GROWTH) < 12 then 'Very Good'
    else 'too much'
    end
    from "SEGMENTATION" S
    In an HTMLDB SQL-Query Report I added Aliases (as requested for HTML) but still gives the error:
    <<Query cannot be parsed, please check the syntax of your query. (ORA-00920: invalid relational operator) >>
    the code:
    select
    case
    when SUM(S.POTENTIAL_GROWTH) pot_growth > 1 and SUM(S.POTENTIAL_GROWTH)pot_growth < 4 then 'Ok'
    when SUM(S.POTENTIAL_GROWTH)pot_growth > 4 and SUM(S.POTENTIAL_GROWTH)pot_growth < 8 then 'SoSo'
    when SUM(S.POTENTIAL_GROWTH)pot_growth > 8 and SUM(S.POTENTIAL_GROWTH)pot_growth < 12 then 'Very Good'
    else 'too much'
    end
    from "SEGMENTATION" S
    any halp from the gurus wud be very much appreciated.
    TIA
    Bernhard

    Still you did not give the alias for the case when column. For that only you should give the alias
    select
    case
    when SUM(S.POTENTIAL_GROWTH) pot_growth &gt; 1 and SUM(S.POTENTIAL_GROWTH)pot_growth &lt; 4 then 'Ok'
    when SUM(S.POTENTIAL_GROWTH)pot_growth &gt; 4 and SUM(S.POTENTIAL_GROWTH)pot_growth &lt; 8 then 'SoSo'
    when SUM(S.POTENTIAL_GROWTH)pot_growth &gt; 8 and SUM(S.POTENTIAL_GROWTH)pot_growth &lt; 12 then 'Very Good'
    else 'too much'
    end "GROWTH"
    from "SEGMENTATION" S

  • Case When Statement and ORA:01722 Invalid number error

    Hi folks, I have posted this under another heading as well under E-business suite so apologies if some you have already seen it but I would really appreciate some help on this one. Any suggestions are most welcome.
    We are trying to put together a calculation that returns the number of days absent an individual has had in a given time period. We need to cater for those absences that started before the period and are closed during it, absence that start during the period and end after it, and those that open and close within it.
    The period is always a rolling 6 months from sysdate.
    This is the calc we have come up with so far which works for some people but we get the invalid number error if the absence includes a half day - so 0.5, 1.5,etc.
    This is probably over complicated but we are not techie at all so are learning as we go!
    We are using the HRMS - Administration - Oracle Human Resources (Core) business area in 10G and the Absence Attendance and Person folders.
    SUM(TO_NUMBER(NVL(( CASE WHEN Absence Attendance.Actual Start Date < TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') THEN ( CASE WHEN Absence Attendance."Actual End Date" > SYSDATE THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) END ) END ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( Absence Attendance.Duration Days ) END ) END ) END ) END ) END ),( DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") )),'999999990D00'))

    Hi,
    It could be that this is because you are using SYSDATE which contains the time as a fraction rather than TRUNC(SYSDATE) which just contains the current time. It could be that your working_dates_between raises this error.
    However, your formula is far more complicated than it needs to be.
    Firstly, you want to look at the date window ADD_MONTHS(TRUNC(SYSDATE), -6) to TRUNC(SYSDATE). Then you want to look at the portion of the absence that falls in the date window. This is GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6)) to LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE)). You may need to add 1 to the absence end date because this is the last day of their absence rather than the date they return. It depends how you calculate the days between the start and end
    date of the absence. You can create calculations for the start and end date of the absences within the 6 months time window. Create calculation AbsenceStart as
    GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6))
    and AbsenceEnd as
    LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE))
    Then you need to only pick up absence that a part of the absence in your 6 month date window. You can use a condition in the workbook or a condition in a case statement to do this. You then need to calculate the difference between these dates and SUM all the values.
    SUM(CASE WHEN AbsenceEnd >= AbsenceStart THEN WORKING_DAYS_BETWEEN(AbsenceStart, AbsenceEnd) END)
    That's it. Not so complicated after all.
    Rod West

  • Case When Statement and ORA:1722 Invalid number error

    Sorry I posted this in the wrong forum - I have the answer now
    Cheers
    HELP!!!
    We are trying to put together a calculation that returns the number of days absent an individual has had in a given time period. We need to cater for those absences that started before the period and are closed during it, absence that start during the period and end after it, and those that open and close within it.
    The period is always a rolling 6 months from sysdate.
    This is the calc we have come up with so far which works for some people but we get the invalid number error if the absence includes a half day - so 0.5, 1.5,etc.
    This is probably over complicated but we are not Techie at all so are learning as we go! We are using the HRMS - Administration - Oracle Human Resources (Core) business area in 10G and the Absence Attendance and Person folders.
    SUM(TO_NUMBER(NVL(( CASE WHEN Absence Attendance.Actual Start Date < TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') THEN ( CASE WHEN Absence Attendance."Actual End Date" > SYSDATE THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) END ) END ) ELSE ( CASE WHEN ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) END ) END ) END ) IS NULL THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),SYSDATE) ) ELSE ( CASE WHEN TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY') >= Absence Attendance.Actual Start Date THEN ( WORKING_DAYS_BETWEEN(TO_DATE(ADD_MONTHS(SYSDATE,-6),'DD-Mon-YYYY'),Absence Attendance."Actual End Date") ) ELSE ( CASE WHEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") ) IS NULL THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( CASE WHEN SYSDATE <= Absence Attendance."Actual End Date" THEN ( WORKING_DAYS_BETWEEN(Absence Attendance.Actual Start Date,SYSDATE) ) ELSE ( Absence Attendance.Duration Days ) END ) END ) END ) END ) END ),( DAYS_BETWEEN(Absence Attendance.Actual Start Date,Absence Attendance."Actual End Date") )),'999999990D00'))
    Edited by: CPearce on Sep 25, 2008 8:03 AM

    Hi,
    It could be that this is because you are using SYSDATE which contains the time as a fraction rather than TRUNC(SYSDATE) which just contains the current time. It could be that your working_dates_between raises this error.
    However, your formula is far more complicated than it needs to be.
    Firstly, you want to look at the date window ADD_MONTHS(TRUNC(SYSDATE), -6) to TRUNC(SYSDATE). Then you want to look at the portion of the absence that falls in the date window. This is GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6)) to LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE)). You may need to add 1 to the absence end date because this is the last day of their absence rather than the date they return. It depends how you calculate the days between the start and end
    date of the absence. You can create calculations for the start and end date of the absences within the 6 months time window. Create calculation AbsenceStart as
    GREATEST(Absence Attendance.Actual Start Date, ADD_MONTHS(TRUNC(SYSDATE), -6))
    and AbsenceEnd as
    LEAST(NVL(Absence Attendance."Actual End Date",TRUNC(SYSDATE)), TRUNC(SYSDATE))
    Then you need to only pick up absence that a part of the absence in your 6 month date window. You can use a condition in the workbook or a condition in a case statement to do this. You then need to calculate the difference between these dates and SUM all the values.
    SUM(CASE WHEN AbsenceEnd >= AbsenceStart THEN WORKING_DAYS_BETWEEN(AbsenceStart, AbsenceEnd) END)
    That's it. Not so complicated after all.
    Rod West

  • SUM(CASE WHEN statement is not returning proper result.

    Hi Folks,
    I'm an end-user on OBIEE 11g creating a custom analysis. I'm trying to SUM a particular case statement: SUM(CASE WHEN "MyMeasureColumn.Here" LIKE '%~25~%' OR "MyMeasureColumn.Here" LIKE '%~26~%' OR "MyMeasureColumn.Here" LIKE '%~27~%' OR "MyMeasureColumn.Here" LIKE '%~28~%' THEN 1 ELSE 0 END)
    The result should be about 6,700 however I am getting only 1,900. Any ideas?

    >
    Ive done a manual validation of the statement (in excel) and know its near 6700. I think it has something to do with the type of data it is?
    >
    Well there are only three possibilities that I can think of.
    1. The data in Excel is different than the data in Oracle
    2. The 'manual validation' is different than what the query that you are using does
    3. Both #1 and #2
    You are the only one with access to the data.
    You are the only one that has any idea what your 'manual validation' was.
    If necessary you will need to use the brute force 'divide and conquer' method.
    Use a small amount of data and compare your 'manual validation to your query results. If they are not the same figure out why.
    If they are the same then repeat the process with another set of the data.

  • CASE vs DECODE - CASE with SUM and All in Page Item is non aggregable

    Hi,
    I'm using Discoverer 9.0.4.
    After switching calculations from DECODE to CASE
    I found out that case gives a non aggregable result when using a Page Item and selecting <All>.
    The calculations
    (SUM x) / (SUM y)
    or
    (x SUM) / (y SUM)
    where x and y are variables, work fine with page item <All>.
    But for example:
    CASE WHEN 1=2 THEN 1 ELSE (SUM x) / (SUM y) END
    gives non-aggregable.
    The same code works with DECODE:
    DECODE(1,2,1,(SUM x) / (SUM y))
    and is aggregable.
    Does anyone know a reason or a way to make it work with CASE?
    Thanks,
    Joao Noronha
    P.S.: I wanted <= comparisons and CASE is the best in simplicity,
    but now I know I can do it with DECODE, still looking ok using LEAST instead of ABS of the difference.

    Hi there
    I think therefore you have answered your own question and determined that using CASE in aggregations is not a good idea. I only threw out the two CASE options as ideas not as solutions, just in case (pardon the pun) one of these worked in your situation.
    Your comment I must say that if it worked it would give a wrong result (the sum of the divisions is not the same as the division of the sums) may give the wrong answer in your case but may be correct in others. It just depends how the items in the folder have been set up. I agree though that SUM(x) / SUM(y) will more often than not give the right answer.
    This discussion about DECODE vs CASE has been going on ever since Oracle introduced CASE as a means of placating a younger breed of user who needed an IF..THEN...ELSE construct and could not get their minds around the intricacies of DECODE. The DECODE is a much more reliable function than CASE because it has been around for a long time allowing Oracle plenty of opportunity to iron the bugs out of it. If I get a chance I will always use a DECODE whenever aggregations are required. However, when no aggregations are in use then I'll use CASE, simply because it's easier for users to work with.
    Unfortunately, users need to work with aggregations and so I don't see any alternative to Plus users having to learn DECODE. Whenever I teach Plus I always teach the users both CASE and DECODE but point out that DECODE has fewer issues that CASE. Oh, and talking of issues, try getting the THEN and ELSE components to return a different datatype. CASE has a fit and will not compile.
    Best wishes and glad you got your issue solved - you did right?
    Regards
    Michael

  • Combine the output and make the sum by article

    In our company we have a lot of small orders (the customer orders every day the same articles) ->
    we make just one bill at the end of the month.
    Most customers don't wont the detail (to much paper) and therefore we have for the bill the possibility to just make the total bill (total per article...).
    so we design the two print program (global bill) and (detail bill) to output RD00,
    when we create a "Credit notes" for such a bill, we will get a  huge amount of paper (output type = BA00) -> so the question was if there was a standard program to combine the output and make the sum by article...
    Thanks.

    A custom print program is what u need, what u can do is create a report program which takes input all the articles ( a range thru selection screen wud do) and then calculate their sum or whatevr according to ur requirement and collate them into an internal table and output a script form thru the report itself, so no need to attach output type and all that.
    кu03B1ятu03B9к

Maybe you are looking for

  • Customizing request:  Table T001L

    Hi, We are currently customizing SAP in two parallell ongoing projects. IN both projects, we must create entries in table T001L (storage locations). But the customizing must be transported at different times. Our question: When the customizing reques

  • Condition VPRS value in operating concern currency.

    Hi, We've encountered the following problem. I was wondering if anyone of you had a similar one and how did you go about it: 1. We've defined second local currency for our company codes = currency type 30 = USD 2. CO-PA stores both company code curre

  • Text tool not working in photoshop CC, all I can see are small boxes event with giant font

    Hi there, I am not sure what is going on here.  I cannot get the Text to show up using the Text tool.  All I can see are tiny boxes even made the font 60 points.  SHould be huge.  Been happening for more than a week and I ran an update via the cloud

  • Scrolling multiple images in scrollPane

    Hey. I want to add multiple images (they are now in movie clips) to a scrollPane, and it's giving me more problems than I think it should. I thought of adding many images to one movie clip, but what I may end up doing is switching the order of the im

  • Please help - I'm not able to download previously purchased music

    Please advice how I can download previously purchased music in iTunes. - The music icon is missing in the Purchased window. Thanks Michael (Denmark)