Conditional MAX or case statement

I am working on a query and need some guidance on how to get the results I need.  Below is an example of the data I'm working with:
ID                  Appl_Nbr               Prog_Nbr           Rating_Cmp  
        Rating_Cmp_Value      ROWN
200346720    01343409            1                        UG_HSADJMP          0.00                            
1
200346720    01343409            1                        UG_HSADJSP         
8.40                              2
Basically, what I am trying to do is when there are 2 rows with the same value, I want UG_HSADJMP.  Otherwise, I want the MAX of the two.  Sorry for the poor description but I didn't know how to explain it better.

Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. And you need to read and download the PDF for: 
https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
>> I am working on a query and need some guidance on how to get the results I need. Below is an example of the data I'm working with: <<
Gee, how rude and useless! Does your boss hold you in such contempt that he gives you pictures of data instead of DDL? 
>> Basically, what I am trying to do is when there are 2 rows with the same value [value of what?], I want UG_HSADJMP. Otherwise, I want the MAX of the two. Sorry for the poor description but I didn't know how to explain it better. <<
Post DDL. Learn why a generic “id” cannot exist in RDBMS; it has to be “<something in particular>_id” if you think about it. The rest of this is also vague. 
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL

Similar Messages

  • Conditional Filters using case statements

    Hello,
    I have a table view which displays Total Quota and Theatre Quota. Against the Total Quota, there are 2 values - Rollover Revenue and Theatre Revenue. Against the Theatre Quota, there is only Theatre Revenue.
    What I want to accomplish is to display only the Rollover Revenue Aggregated Quarterly number whenever there is a Total Quota number and not display the Theatre Revenue number
    In the table view,
    Year Name     Quarter Name     Quarter Name Sort     Person Region     Quota Name     Quarterly Quota     Credit Amount     QTD Attainment     Credit Type Name
    YEAR-2012     QTR-1-2012     QTR-1-2012     750     Total Quota     6,128,500     5,492,081     89.62%     Rollover Revenue Aggregated Quarterly
                   750          6,128,500     5,344,000     87.20%     Theatre Revenue
         QTR-2-2012     QTR-2-2012     750     Total Quota     5,922,500     5,890,264     99.46%     Rollover Revenue Aggregated Quarterly
                   750          5,922,500     6,120,000     103.33%     Theatre Revenue
         QTR-3-2012     QTR-3-2012     750     Total Quota     5,716,500     0     0.00%     
         QTR-4-2012     QTR-4-2012     750     Total Quota     5,510,500     0     0.00%     
    I used an example in the following link:
    http://oraclebizint.wordpress.com/2008/02/06/oracle-bi-ee-101332-conditional-filters-using-case-statements-in-filters/
    and applied the example in my scenario:
    CASE WHEN Quota."Quota Name" = 'Total Quota' THEN "Credit Type"."Credit Type Name" ELSE 'Dummy' END != 'Theatre Revenue'
    I still get duplicate rows.
    Thanks.

    Could you suggest any solutions for this problem where I can conditionally hide the number only for a certain type of data and not for all type of data?
    Thanks.

  • Checking for the condition types using case statement

    hi folks,
    I have a lot of condition types that I have to check for and I am using case statement to do that. The code goes like this.
    case wac-kschl.
            when 'ZRAT' OR 'ZAGR' OR 'ZRCR' OR
                  'Y098' OR 'Y007' OR 'ZREW' OR 'Y106'        OR 'ZTSR' OR 'Y127' OR 'Y125' OR 'Y126' OR 'Y124' OR 'Y157' OR 'Y092' OR 'Y085' OR 'Y090' OR 'ZMZD'
    OR 'Y215' OR 'Y214' OR 'Y111' OR 'ZC$D' OR 'ZAUD'.
    up till here it is working on errors and when I add few more condition types to the case statement it is throwing the error.
    I have to check for all the condition types out here.
    How can I correct it? Is there a better way to do it?
    thanks
    Santhosh

    Hi Santhosh,
    I think that your CASE statement has a flaw. The line length of one of the lines is too large. You need to insert a carriage-return to shorten it (or press the button 'Pretty Printer').
    The code would look nicer like this:[code]  CASE wac-kschl.
        WHEN 'ZRAT' OR 'ZAGR' OR 'ZRCR' OR 'Y098' OR 'Y007' OR 'ZREW'
          OR 'Y106' OR 'ZTSR' OR 'Y127' OR 'Y125' OR 'Y126' OR 'Y124'
          OR 'Y157' OR 'Y092' OR 'Y085' OR 'Y090' OR 'ZMZD' OR 'Y215'
          OR 'Y214' OR 'Y111' OR 'ZC$D' OR 'ZAUD' OR 'Z001' OR 'Z002'
          OR 'Z003' OR 'Z004' OR 'Z005' OR 'Z006' OR 'Z007' OR 'Z008'
          OR 'Z009' OR 'Z010' OR 'Z011' OR 'Z012' OR 'Z013' OR 'Z014'.
        Do your thing here
          WRITE: / 'OK'.
        WHEN OTHERS.
          WRITE: / 'NOT OK'.
      ENDCASE.[/code]If this will not work for you, you could try a different approach:[code]* Local definition
      DATA:
        var_list(1024).
    Build variable string for checking
      CONCATENATE 'ZRAT ZAGR ZRCR Y098'
                  'Y007 ZREW Y106 ZTSR'
                  'Y127 Y125 Y126 Y124'
                  'Y157 Y092 Y085 Y090'
                  'ZMZD Y215 Y214 Y111'
                  'ZC$D ZAUD'
             INTO var_list
        SEPARATED BY space.
    Check if the correct value is supplied
      IF var_list CS wac-kschl.
      Do your thing here
        WRITE: / 'OK'.
      ENDIF.[/code]Hope this helps you a bit.
    Regards,
    Rob.

  • Count(*) that is conditioned and then case statement of result

    Hi: This is table_1 :
    with Table_1
    as
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') incident_date, 2005 Policy_no  from dual
          union all
          select to_date('04-MAR-2009 12:12','DD-MON-YYYY HH24:MI') incident_date, 2009 Policy_no  from dual
          union all
          select to_date('25-JUL-2010 23:30','DD-MON-YYYY HH24:MI') incident_date, 2013 Policy_no  from dual
    select * from Table_1
    with Table_2
    as
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 300 agr_line_no, 'CELL'  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 400 agr_line_no, 'CAR'  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 300 agr_line_no, 'BEACH'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 400 agr_line_no, 'CAR'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
          union all
          select to_date('20-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('29-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2013 Policy_no, 676 agr_line_no, 'CAR'  from dual
    select * from Table_2I need to perform a count(*) on agr_line_no for the condition that there are other objects other than 'CAR' ie so forpolicy_no = 2005 the count must be 4.
    Table_1 must join to table_2 on Policy_no and all columns in table_1 must be selected as well as an additional column called Object_many.
    Object_many is based on the count(*) from table_2 ie if the count is > 1 then 'Y' must be returned else 'N'
    Also there must be a where clause ie where table_1.incident_date is between table_2 .cover_start_date and table_2 .cover_end_date
    Final Result Should look something like this
    with Table_1
    as
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') incident_date, 2005 Policy_no, 'Y' Object_many from dual
          union all
          select to_date('04-MAR-2009 12:12','DD-MON-YYYY HH24:MI') incident_date, 2009 Policy_no, 'Y' Object_many from dual
          union all
          select to_date('25-JUL-2010 23:30','DD-MON-YYYY HH24:MI') incident_date, 2013 Policy_no, 'N' Object_many from dual
    select * from Table_1Thanks in advance!!
    Banner:
    Oracle Database 11g 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

    with Table_1
    as
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') incident_date, 2005 Policy_no  from dual
          union all
          select to_date('04-MAR-2009 12:12','DD-MON-YYYY HH24:MI') incident_date, 2009 Policy_no  from dual
          union all
          select to_date('25-JUL-2010 23:30','DD-MON-YYYY HH24:MI') incident_date, 2013 Policy_no  from dual
    ), Table_2
    as
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 300 agr_line_no, 'CELL'  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 400 agr_line_no, 'CAR'  from dual
          union all
          select to_date('01-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('04-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2005 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 100 agr_line_no, 'BIKE' object  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 200 agr_line_no, 'BUCKS'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 300 agr_line_no, 'BEACH'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 400 agr_line_no, 'CAR'  from dual
          union all
          select to_date('01-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('09-MAR-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2009 Policy_no, 500 agr_line_no, 'HOUSE'  from dual
          union all
          select to_date('20-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_start_date, to_date('29-JUL-2009 20:10','DD-MON-YYYY HH24:MI') Cover_end_date, 2013 Policy_no, 676 agr_line_no, 'CAR'  from dual
    -- end-of-test-data
    select
    table_1.policy_no,
    max(table_1.incident_date) incident_date,
    case when count(table_2.agr_line_no) > 1 then 'Y' else 'N' end object_many
    from table_1
    left outer join table_2
       on table_2.policy_no = table_1.policy_no
       and table_2.cover_start_date <= table_1.incident_date
       and table_2.cover_end_date >= table_1.incident_date
       and table_2.object != 'CAR'
    group by
    table_1.policy_no
    order by
    table_1.policy_no;I have assumed policy_no to be primary key in Table_1 - therefore max(table_1.incident_date) will work.
    If you have an index on Table_2 on the cover dates then the above construction might be better than a between. Otherwise between will be quite OK ;-)

  • How to manipulate arrays using case statements and boolean conditions?

    In the vi that is attached I am trying to compare two values in two different arrays and delete the value that is equal to zero.  The values of each array are rounded to the closest integer.  Then I attempted to apply the ">0" boolean as the condition for my case statement, but I am getting an error.  Any tips on manipulating arrays with case statements?
    Attachments:
    Patient Movement Monitoring.vi ‏141 KB

    Thank you!!! that was a huge help. I don't think I need the case structures at all.  The next part of the code compares the 4 values in the array. 
    If columns 0 and 1 are both positive -> output 1
    If column 0 is negative and 1 is positive -> output 2
    If columns 0 and 1 are both negative -> output 3
    If column 0 is positive and 1 is negative -> output 4
    (0 is x-axis value, 1 is y-axis value.....outputs are assigning quadrants to the combination)
    Only one of the "AND" booleans will return true for each index.  Is there a way to initialize another array of outputs 1-4 depending on which AND returns true?
    Attachments:
    Patient Movement Monitoring.vi ‏144 KB

  • Performance in a case statement

    Hi
    I have a query like this -
    SELECT
    MAX(CASE WHEN field1 = 'xyz' THEN field2 END) AS alias1,
    MAX(CASE WHEN field2 = 'abc' THEN field2 END) AS alias2
    FROM table1
    WHERE
    field1 = 'xyz' and
    field2 = 'abc' and
    some fixed condition
    Please note that the conditions in where clauses are same as corresponding case statements apart from one fixed condition.
    In the actual query there are ~15 such conditions and 15 case statements for each selected field.
    I have put the where clause to reduce the number of rows getting returned from the table.
    My question is - what happens in terms of performance if I remove the where clause here. In other words will above query perform better or same as this
    SELECT
    MAX(CASE WHEN field1 = 'xyz' THEN field2 END) AS alias1,
    MAX(CASE WHEN field2 = 'abc' THEN field2 END) AS alias2
    FROM table1
    WHERE
    some fixed condition
    Thanks
    Kumar

    It really depends on what you query is trying to accomplish. The two queries are not the same. Your first will select rows where both field1 = 'xyz' and field2 = 'abc', while the second would also return rows where, for example, field1 = 'xyz' and field2 = 'def'. In which case, your alias2 would be null.
    John

  • CASE Statement in Where Condition with Multi Valued parameter in SSRS

    Hi All,
    I am little confused while using CASE statement in Where condition in SSRS. Below is my scenario:
    SELECT
    Logic here
    WHERE
    Date IN (@Date)AND
    (CASE
    WHEN NAME LIKE 'ABC%' THEN 'GROUP1'
    WHEN ID IN ('123456', '823423','74233784') THEN 'GROUP2'
    WHEN ABC_ID IS NULL THEN 'GROUP3'
    ELSE 'GROUP4'
    END ) IN (@GROUP)
    So above query uses WHERE condition with CASE statement from @GROUP parameter. I want to pass this parameter as multi- valued parameter and hence I have used CASE statement IN (@GROUP).
    For @Date one dataset will pass the available and default values and
    for @GROUP parameters, another dataset will pass the available and default values.
    But this is not working as expected. Please suggest me where I am making mistake in the query.
    Maruthu | http://sharepoint-works.blogspot.com

    Hi Maruthu,
    According to your description, I create a sample report in my local environment. It works as I expected. In your scenario, if the selected values from the Date parameter contains some of the Date field values, the selected values from the GROUP parameter
    contains some of GROUPS (‘GROUP1’,’GROUP2’,’GROUP3’,’GROUP4’) and the corresponding when statement is executed , then the dataset returns the corresponding values.
    In order to trouble shoot this issue, could you tell us what results are you get and what’s your desired results? If possible, you can post the sample data with sample dataset, then we can make further analysis and help you out.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • Evaluation of condition clause in whehe clause using case statement

    Hi,
    I have a scenario where in I have to evaluate two different conditions based on the parameter value. Is it possible to evaluate the conditiond based on the parameter value with case statement in where clause.
    Example:
    select A,B,C,D from X,Y,Z
    where
    cond1 and
    cond2 and
    case when param='T' then cond3 else cond4 end
    Here param is an external parameter passed to the query.
    cond3 is something like Y.deptno in(10,30,50,70)
    cond4 is something like Y.deptno in (20,40,60,70,80,90)
    Your responce will be appriciated.
    Regards,
    Varma

    You can place the case statement as a condition, however it depends what context you want it in, for example;
    this will work:
    1* select 1 from dual where (case when 1=1 then 1 end) = 1 and 2=2
    QL> /
            1
            1but this will not;
    SQL> select 1 from dual where case when 1=1 then 1 end and 2=2;
    select 1 from dual where case when 1=1 then 1 end and 2=2
    ERROR at line 1:
    ORA-00920: invalid relational operatorSo, if you're expecting the condition to evaluate to a true/false, so if you mean soemthing like
    when TRUE, then it won't work!
    P;

  • Conditional case statement help.!

    Hello Forum Members,
    I have a table with stores order along with order dates and other information. I have a requirement to write a query to get the nominations from that order's table according to the logic mentioned below.
    Nom1 =  Initial order date and next order within 7 days from initial order date then its considered as R1
    Nom2 = next activity within 7 seven on the same ord_id from its R1 date , this goes on as the order dates increases for same  or single ord_no
    Final Rn  is Count (Rn)
    Rn %-- Count(Rn)  / Count Distinct ( ord_no ) * 100
    Example data output:
    ORD_DATE
    ORD_ID
    1_of_x
    2_of_x
    3_of_x
    4_of_x
    5_of_x
    6_of_x
    Final RnX
    Rn%
    7/10/2013
    10095V1
    1
    0
    0
    0
    0
    0
    8/3/2013
    10095V1
    1
    0
    0
    0
    0
    0
    8/8/2013
    10095V1
    0
    1
    0
    0
    0
    0
    8/12/2013
    10095V1
    0
    0
    1
    0
    0
    0
    9/6/2013
    10095V1
    1
    0
    0
    0
    0
    0
    DDL:
    create table xx_ord_noms
    ord_no varchar(30),
    ord_date date,
    ord_id varchar2(30)
    Sample Data:
    INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('07/10/2013','MM/DD/YYYY'), '10091');
    INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('08/03/2013','MM/DD/YYYY'), '10091');
    INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('08/08/2013','MM/DD/YYYY'), '10091');
    INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('08/12/2013','MM/DD/YYYY'), '10091');
    INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('09/06/2013','MM/DD/YYYY'), '10091');
    commit;
    I was planning to doing something like this but I am not getting the logic for R1 .. R7
    select ord_no,
           ord_date,
           ord_id,
           prev_dt,
           (to_date(ord_date) - to_date(prev_dt)) data_diff,
           case
             when prev_dt is null or
                  (to_date(ord_date) - to_date(prev_dt)) < 7 then
              'R1'
             when (to_date(ord_date) - to_date(prev_dt)) > 7 THEN
              'R2'
             ELSE
              nULL
           END
      FROM (select a.*,
                   (SELECT MAX(tmp2.ord_date)
                      FROM xx_ord_noms tmp2
                     WHERE a.ord_id = tmp2.ord_id
                       AND tmp2.ord_date < a.ord_date) prev_dt
              from xx_ord_noms a);
    Could someone give me advises or suggestions on how we can get the expected output.
    Thanks in advance.

    LAG and LEAD are good when you want to look forward or back a fixed number of rows, but in this case you don't know how far back you'll need to go.
    I've got a solution. it calculates the Rn as a number, not a column position, but you can transform that with CASE statements if you want. I did it in two main steps:
    First I calculated the Rn by comparing the date of the current and previous row. To do that, I generated a row number using the row_number()  analytic function:
    select ord_no,
           ord_date,
           ord_id,
           numrow
    , Rn
    from xx_ord_noms
    model
      partition by (ord_no)
      dimension by (row_number() over (partition by ord_no order by ord_date)  numrow)
      measures (ord_date, 0 Rn, ord_id )
      rules update
        rn[1] = 1,
        rn[ANY] = CASE when trunc(ord_date[cv()]) - trunc(ord_date[cv() - 1])  < 7 then rn[cv()-1]+1 else 1 end
    ORD_NO     ORD_DATE             ORD_ID         NUMROW         RN
    10091      10-Jul-2013 00:00:00 10091               1          1
    10091      03-Aug-2013 00:00:00 10091               2          1
    10091      08-Aug-2013 00:00:00 10091               3          2
    10091      12-Aug-2013 00:00:00 10091               4          3
    10091      06-Sep-2013 00:00:00 10091               5          1
    Then I needed to identify all the rows of each series. I defined firstDt as the date of the first item in the series:
    firstDt[1] = ord_date[cv()]
    firstDt[ANY] = CASE when rn[cv()]= 1 then ord_date[cv()] else firstDt[cv()-1] end
    With that, the FinalRn is just an analytic max(rn) over (partition by firstDt)
    I'm not clear on what R% is. Your descriptions are unclear and I think they're inconsistent.
    select ord_no, ord_date, ord_id
    , Rn, finalRn, 100*finalRn/totCnt PCT1, 100 * totRn/totCnt PCT2
    , totRn
    from xx_ord_noms
    model
      partition by (ord_no)
      dimension by (row_number() over (partition by ord_no order by ord_date)  numrow)
      measures (ord_date, 0 Rn, ord_id, to_date(null) firstDt, 0 finalRn, 0 totRn, 0 totCnt )
      rules upsert all
        rn[1] = 1
      , rn[ANY] = CASE when trunc(ord_date[cv()]) - trunc(ord_date[cv() - 1])  < 7 then rn[cv()-1]+1 else 1 end
      , firstDt[1] = ord_date[cv()]
      , firstDt[ANY] = CASE when rn[cv()]= 1 then ord_date[cv()] else firstDt[cv()-1] end
      , finalRn[ANY] = max(rn) over (partition by firstDt)
      , totCnt[ANY] = count(*) over()
      , totRn[ANY] = count(case when rn = 1 then 1 else null end) over ()
    ORD_NO     ORD_DATE             ORD_ID             RN    FINALRN       PCT1       PCT2      TOTRN
    10091      10-Jul-2013 00:00:00 10091               1          1         20         60          3
    10091      03-Aug-2013 00:00:00 10091               1          3         60         60          3
    10091      08-Aug-2013 00:00:00 10091               2          3         60         60          3
    10091      12-Aug-2013 00:00:00 10091               3          3         60         60          3
    10091      06-Sep-2013 00:00:00 10091               1          1         20         60          3
    Regards,
    David

  • Max Case Statement

    Hi,
    Can anyone help with the case statement in the query below, is it possible to check the date using MIN in a case statement? The MIN date needs to be split by ID.
    SELECT id,
    name,
    MIN(to_date(payment_date, 'YYYYMMDD')) first_date,
    MAX(to_date(payment_date, 'YYYYMMDD')) last_date,
    CAST(SUM(payment_amount) AS DOUBLE PRECISION) amount,
    ABS(CAST(SUM(
    CASE
    WHEN payment_amount < 0 and to_date(payment_date, 'YYYYMMDD') > MIN(to_date(payment_date, 'YYYYMMDD')) THEN payment_amount
    END) AS DOUBLE PRECISION)) credit
    FROM test
    group by id, name
    ORA-00937: not a single-group group function
    00937. 00000 - "not a single-group group function"
    I am trying to acheive the following in the case statement:
    CASE
    WHEN payment_amount < 0 and payment_date > first_date THEN payment_amount
    END

    Hi,
    sliderrules wrote:
    Below is a test script:
    create table test(
    id number,
    name varchar2(10),
    payment_date date,Is payment_date a DATE or a string?
    As the name implies <b>TO_</b>DATE converts a string TO a DATE. If payment_date is already a DATE, there's no need to convert it.
    payment_amount number
    insert into test values(001, 'HK', '20-SEP-10', 69.36);If payment_date is a DATE, don't try to insert a string (such as '20-SEP-10') into that column. Use TO_DATE here, not in your query.
    insert into test values(001, 'HK', '23-SEP-10', 235);
    insert into test values(001, 'HK', '23-SEP-10', 589.36);
    insert into test values(001, 'HK', '14-OCT-10', -743);
    insert into test values(001, 'HK', '18-OCT-10', -39.68);
    insert into test values(002, 'DU', '18-JUL-11', -56.36);
    insert into test values(002, 'DU', '23-SEP-11', 48.96);
    insert into test values(002, 'DU', '14-NOV-11', -529.63);
    insert into test values(002, 'DU', '14-FEB-12', -15.23);
    insert into test values(002, 'DU', '14-FEB-12', 896.21);
    insert into test values(002, 'DU', '18-APR-12', -55.14);
    insert into test values(003, 'GS', '11-APR-10', 36.69);
    insert into test values(003, 'GS', '23-SEP-10', 47.96);
    insert into test values(003, 'GS', '14-FEB-11', 14.23);
    insert into test values(003, 'GS', '14-JAN-12', -19.23);
    insert into test values(003, 'GS', '16-FEB-12', -63.22);
    insert into test values(003, 'GS', '18-JUL-12', -78.52);
    Expected Results:
    ID- 001
    NAME- HK
    FIRST_DATE- 20-Sep-10
    LAST_DATE- 18-Oct-10
    AMOUNT- 111.04
    CREDIT AMOUNT- -782.68
    ID- 002
    NAME- DU
    FIRST_DATE- 18-Jul-11
    LAST_DATE- 18-Apr-12
    AMOUNT- 288.81
    CREDIT AMOUNT- -656.36Why do you want -656.36 above? Why not -600.00?
    ID- 003
    NAME- GS
    FIRST_DATE- 11-Apr-10
    LAST_DATE- 18-Jul-12
    AMOUNT- -62.09
    CREDIT AMOUNT- -160.97Do you really want the results unpivoted like that, or do you want them in a tabular format, like this?
            ID NAME       FIRST_DAT LAST_DATE     AMOUNT     CREDIT
             1 HK         20-SEP-10 18-OCT-10     111.04    -782.68
             2 DU         18-JUL-11 18-APR-12     288.81       -600
             3 GS         11-APR-10 18-JUL-12     -62.09    -160.97If you want the unpivoted output, your Oracle version is even more important.
    ABS always returns a positive number whether you pass it a positive or a negative number. If credit is to be less than 0, then don't use ABS.
    SELECT id,
    name,
    MIN(to_date(payment_date, 'YYYYMMDD')) first_date,
    MAX(to_date(payment_date, 'YYYYMMDD')) last_date,
    CAST(SUM(payment_amount) AS DOUBLE PRECISION) amount,
    ABS(CAST(SUM(
    CASE
    WHEN payment_amount < 0 and to_date(payment_date, 'YYYYMMDD') > MIN(to_date(payment_date, 'YYYYMMDD')) THEN payment_amount
    END) AS DOUBLE PRECISION)) credit
    FROM test
    group by id, name
    ORA-00937: not a single-group group function
    00937. 00000 - "not a single-group group function"That looks like the same query you started with.
    I am trying to acheive the following in the case statement:
    CASE
    WHEN payment_amount < 0 and payment_date > first_date THEN payment_amount
    ENDThen compute first_date in a sub-query, as I showed earlier.

  • Conditions in case statement

    Hello :
    Is it possible to make something like this conditions in a
    Case statment?:
    Case mycondition of
    (mycondition >= 10 and mycondition <= 50 ):
    --do this
    etc...
    end Case
    I know that can use If conditional but is it possible of the
    above?
    Thanks in advance

    By the way, you MUST use parenthesis around each case when
    you use this
    format.
    Craig Wollman
    Lingo Specialist
    Word of Mouth Productions
    212-928-9581
    www.wordofmouthpros.com
    "Anne" <[email protected]> wrote in message
    news:ekjkd0$vn$[email protected]..
    > Hello :
    >
    > Is it possible to make something like this conditions in
    a Case statment?:
    >
    > Case mycondition of
    > (mycondition >= 10 and mycondition <= 50 ):
    > --do this
    > .
    > .
    >
    > etc...
    > end Case
    >
    > I know that can use If conditional but is it possible of
    the above?
    >
    > Thanks in advance
    >
    >

  • Case statement insert not working

    This cursor is retrieving data for each pidm
    SPRIDEN_PIDM     ACTIVITY1     ACTIVITY2     ACTIVITY3     ACTIVITY4     ACTIVITY5     ACTIVITY6
    2287953                        RA      RC      RF     RJ       RN      RU
    CURSOR activities_cursor
          IS
             SELECT spriden_pidm,
                    REGEXP_SUBSTR (szcasup_activities,
                                   '[[:alpha:]]+',
                                   1,
                                   1
                                  ) activity1,
                    REGEXP_SUBSTR (szcasup_activities,
                                   '[[:alpha:]]+',
                                   1,
                                   2
                                  ) activity2,
                    REGEXP_SUBSTR (szcasup_activities,
                                   '[[:alpha:]]+',
                                   1,
                                   3
                                  ) activity3,
                    REGEXP_SUBSTR (szcasup_activities,
                                   '[[:alpha:]]+',
                                   1,
                                   4
                                  ) activity4,
                    REGEXP_SUBSTR (szcasup_activities,
                                   '[[:alpha:]]+',
                                   1,
                                   5
                                  ) activity5,
                    REGEXP_SUBSTR (szcasup_activities,
                                   '[[:alpha:]]+',
                                   1,
                                   6
                                  ) activity6              
               FROM saturn_midd.szcasup, saturn.spriden
              WHERE szcasup_common_appl_id = spriden_id
                AND spriden_ntyp_code = 'CAPP';Then I fetched the results in variadles
    FETCH activities_cursor
              INTO v_pidm, v_activity1, v_activity2, v_activity3, v_activity4,
                   v_activity5,v_activity6;
    Created a loop with a case statement
    LOOP
             FETCH activities_cursor
              INTO v_pidm, v_activity1, v_activity2, v_activity3, v_activity4,
                   v_activity5,v_activity6;
             EXIT WHEN activities_cursor%NOTFOUND;
             IF activities_cursor%FOUND
             THEN
                CASE
                   WHEN (v_activity1 IS NOT NULL)
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity1,
                                      sysdate
                    WHEN (v_activity2 IS NOT NULL)
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity2,
                                      sysdate
                   WHEN (v_activity3 IS NOT NULL)
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity3,
                                      sysdate
                    WHEN (v_activity4 IS NOT NULL)
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity4,
                                      sysdate
                END CASE;
             END IF;
          END LOOP;
    It is working just for one activity (activity1) The variable definition is correctv_activity1 sorints.sorints_ints_code%TYPE;
    v_activity2 sorints.sorints_ints_code%TYPE;
    v_activity3 sorints.sorints_ints_code%TYPE;
    v_activity4 sorints.sorints_ints_code%TYPE;
    v_activity5 sorints.sorints_ints_code%TYPE;
    v_activity6 sorints.sorints_ints_code%TYPE;
    Edited by: peace4all on Aug 16, 2009 9:42 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    peace4all wrote:
    It is working just for one activity (activity1) The variable definition is correct
    Well, what else did you expect? CASE evaluates brances starting first one. If WHEN condition is not met it continues to next one. As soon as WHEN condition is met, THEN clause is executed after that CASE exits. You need to use IF statement, not case:
    with t as (
               select to_date('01/11/2005','dd/mm/yyyy') Start_Date,to_date('13/11/2005','dd/mm/yyyy') End_Date,13 Days,'CMP1' Component,1000.00 Amount from dual union all
               select to_date('01/11/2005','dd/mm/yyyy'),to_date('13/11/2005','dd/mm/yyyy'),13,'CMP2',826.67 from dual union all
               select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP1',413.33 from dual union all
               select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP2',166.67 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP1',633.00 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP2',111.00 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP3',22.00 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP4',333.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP1',2480.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP2',1488.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP3',496.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP4',200.00 from dual union all
               select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP1',522.00 from dual union all
               select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP2',166.00 from dual union all
               select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP3',242.00 from dual
    SELECT  P_Month,
            SUM(LEAST(LAST_DAY(P_Month),end_date) - GREATEST(P_Month,start_date) + 1) Total_Days
      FROM  t,
             SELECT  ADD_MONTHS(first_month,LEVEL - 1) P_Month
               FROM  (
                      SELECT  TRUNC(MIN(start_date),'MM') first_month,
                              TRUNC(MAX(end_date),'MM') last_month
                        FROM  t
               CONNECT BY LEVEL <= MONTHS_BETWEEN(last_month,first_month) + 1
      WHERE P_Month BETWEEN TRUNC(start_date,'MM') and end_date
      GROUP BY P_Month
      ORDER BY P_Month
    with t as (
               select to_date('01/11/2005','dd/mm/yyyy') Start_Date,to_date('13/11/2005','dd/mm/yyyy') End_Date,13 Days,'CMP1' Component,1000.00 Amount from dual union all
               select to_date('01/11/2005','dd/mm/yyyy'),to_date('13/11/2005','dd/mm/yyyy'),13,'CMP2',826.67 from dual union all
               select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP1',413.33 from dual union all
               select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP2',166.67 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP1',633.00 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP2',111.00 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP3',22.00 from dual union all
               select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP4',333.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP1',2480.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP2',1488.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP3',496.00 from dual union all
               select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP4',200.00 from dual union all
               select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP1',522.00 from dual union all
               select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP2',166.00 from dual union all
               select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2006','dd/mm/yyyy'),381,'CMP3',242.00 from dual
    SELECT  P_Month,
            SUM(LEAST(LAST_DAY(P_Month),end_date) - GREATEST(P_Month,start_date) + 1) Total_Days
      FROM  t,
             SELECT  ADD_MONTHS(first_month,LEVEL - 1) P_Month
               FROM  (
                      SELECT  TRUNC(MIN(start_date),'MM') first_month,
                              TRUNC(MAX(end_date),'MM') last_month
                        FROM  t
               CONNECT BY LEVEL <= MONTHS_BETWEEN(last_month,first_month) + 1
      WHERE P_Month BETWEEN TRUNC(start_date,'MM') and end_date
      GROUP BY P_Month
      ORDER BY P_Month
    LOOP
             FETCH activities_cursor
              INTO v_pidm, v_activity1, v_activity2, v_activity3, v_activity4,
                   v_activity5,v_activity6;
             EXIT WHEN activities_cursor%NOTFOUND;
             IF v_activity1 IS NOT NULL
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity1,
                                      sysdate
              ELSIF v_activity2 IS NOT NULL
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity2,
                                      sysdate
              ELSIF v_activity3 IS NOT NULL
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity3,
                                      sysdate
              ELSIF v_activity4 IS NOT NULL
                   THEN
                      INSERT INTO saturn.sorints
                                  (sorints_pidm, sorints_ints_code,
                                   sorints_activity_date)
                                   values
                                    v_pidm,
                                     v_activity4,
                                      sysdate
              END IF;
          END LOOP;SY.

  • Help With A Case Statement With Multiple Variables

    I apologize if this is the incorrect Forum for this type of question, but it was the closest one that I could find. I'm pretty new with SQL and am stuck on this issue. I have roughly 26 dates that I need to compare to one another. Each date is tied to a step code. I also have a Stop value that is tied directly to the "max date" of the step codes. So, I need to compare 30 dates against one another to 1st - ID the max date; 2nd - ID if the Stop value is correct; 3rd - if the stop value is incorrect, identify what the correct value would be.
    At first, this seemed like it wouldn't be that hard. I wrote a query that found the max date for each step code. Then I realized that multiple step codes could have the same date. So, I tried using this case statement, but I did not get the expected results. Is there a more efficient way of getting what I need? This code seems like it's not necessary and probably the source of my issue.
    CASE
    WHEN FS25.ACTUAL_COMPLETION_DATE > FS.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS1.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS2.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS3.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS4.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS5.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS6.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS7.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS8.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS9.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS10.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS11.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS12.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS13.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS14.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS15.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS16.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS17.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS18.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS19.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS20.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS21.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS22.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS23.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS24.ACTUAL_COMPLETION_DATE AND L.FORECLOSURE_STOP_CODE <= '8' THEN '9'
    ELSE 'UH OH'
    END AS "CHANGE FC STOP TO"
    Any assistance is appreciated!

    I think Igor pointed out a working solution before.
    Applying it at your examples (you missed the operator after STOP_CODE, I assume it =):
    CASE
    WHEN FS25 = GREATEST(FS25, FS24, FS23) AND STOP_CODE = '9' THEN '9'
    ELSE 'UH OH'
    END AS 'CHANGE STOP CODE TO'
    {code}
    Be careful at the second example. You are checking:
    {code:sql}
    FS25 > FS24 OR FS25 IS NOT NULL AND FS24 IS NULL AND FS25 > FS23
    OR
    FS25 IS NOT NULL AND FS23 IS NULL AND STOP_CODE = '9'
    {code}
    Remember that AND has higher priority among operators than OR so if FS25 is greater than FS24 and FS23 the condition will be true even if STOP_CODE is not equal 9.
    Regards.
    Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Case statement assistance

    I am using a case statement in the following script, so that I can return two seperate columns in my item master query, one for Frozen Cost and one for Actual Cost. I am having trouble with the else close. If the records for frozen are other then frozen, I do not want to have them returned as part of the records and same thing for Actual, if they are not actual I do not want them returned. Any help is appreciated. I am running on 10.2.0.4.0 for the database and for the app layer we are at 11.5.10.2.
    SELECT MSI.INVENTORY_ITEM_ID INVENTORY_ITEM_ID
    , msi.segment1 ITEM
    , MSI.DESCRIPTION DESCRIPTION
    , msi.inventory_item_status_code INVENTORY_ITEM_STATUS_CODE
    , msi.item_type ITEM_TYPE
    , max (mirb.revision) REVISION
    , MSI.PRIMARY_UOM_CODE PRIMARY_UOM_CODE
    , msi.shippable_item_flag SHIPPABLE_ITEM_FLAG
    , NVL (CIC.ORGANIZATION_ID, '0') ORGANIZATION_ID
    ,CASE when cct.cost_type in 'Frozen' then cic.item_cost else 0 end as FROZEN_COST
    ,CASE when cct.cost_type in 'ACTUAL' then cic.item_cost else 0 end as ACTUAL
    , cct.cost_type COST_TYPE
    FROM CST_ITEM_COSTS CIC
    , CST_COST_TYPES CCT
    , MTL_SYSTEM_ITEMS MSI
    , MTL_DEFAULT_CATEGORY_SETS MDC
    , MFG_LOOKUPS LU1
    , mtl_item_revisions_b mirb
    WHERE CCT.COST_TYPE_ID(+) = CIC.COST_TYPE_ID
    AND CIC.INVENTORY_ITEM_ID(+) = MSI.INVENTORY_ITEM_ID
    AND CIC.ORGANIZATION_ID (+) = MSI.ORGANIZATION_ID
    AND MSI.COSTING_ENABLED_FLAG = 'Y'
    AND msi.inventory_item_id = mirb.inventory_item_id
    AND msi.organization_id = mirb.organization_id
    AND LU1.LOOKUP_CODE(+) = MSI.PLANNING_MAKE_BUY_CODE
    /* the outside join below, speeds up query, not sure why */
    AND LU1.LOOKUP_TYPE(+) = 'MTL_PLANNING_MAKE_BUY'
    and cct.cost_type = 'Frozen'
    AND msi.organization_id = 4
    group by CIC.ROWID, MSI.INVENTORY_ITEM_ID, MSI.SEGMENT1, MSI.DESCRIPTION, MSI.INVENTORY_ITEM_STATUS_CODE,
    MSI.PRIMARY_UOM_CODE, MSI.SHIPPABLE_ITEM_FLAG, CIC.ORGANIZATION_ID, MSI.ITEM_TYPE, CIC.ITEM_COST, CIC.MATERIAL_COST, cct.cost_type

    Hi,
    The CASE expressions look good to me, except that you need parentheses after the keyword IN. (See 'Frozen' below.)
    If you're only comparing to one item, as you are here, you can use = instead of IN. (See 'ACTUAL' below.)
    ,      CASE
                 WHEN  cct.cost_type IN ('Frozen' )
              THEN  cic.item_cost
              ELSE  0
           END     AS frozen_cost
    ,      CASE
                 WHEN  cct.cost_type = 'ACTUAL'
              THEN  cic.item_cos
              ELSE  0
           END       AS actualRemember to capitalize things inside quotes carefully. The code above is looking for 'Frozen' with only one capital letter, the 'F', but 'ACTUAL' with all capital letters. Is that what you want?
    It looks like you're doing a GROUP BY in this query. If so, you'll have to copy these CASE expresssions into the GROUP BY clause, or use them only inside an aggregate function. For example:
    ,      SUM ( CASE
                        WHEN  cct.cost_type IN ('Frozen' )
                     THEN  cic.item_cost
                     ELSE  0
                 END
               )         AS frozen_cost
    ,      SUM ( CASE
                        WHEN  cct.cost_type = 'ACTUAL'
                     THEN  cic.item_cos
                     ELSE  0
                 END
               )           AS actualSince your WHERE clause includes the condition
    cct.cost_type = 'Frozen'I would expect the 1st CASE expression to include all rows, and the 2nd to include no rows. If that's the situation, why have CASE expressions at all?
    I hope that answers your question.
    If not, post a simplified query, involviong only these CASE exoressions and the GROUP BY.
    Post CREATE TABLE and INSERT statements for a little sample data (include only the columns and tables in the simplified query). Also post the results you want from that sample data.
    Edited by: Frank Kulash on Aug 12, 2010 5:10 PM
    Corrected bad nesting: "END" should be before ")".

  • CASE statement in Calculated column

    Hi Frzz,
    I have below requirement in Calculated column with CASE statement. Could some one help me how to achieve this with case statement.
    String  =   0Hello
                    01Hello
                    012Hello
                    0123Hello
    If  1st Character of the string is '0' then  -  0Hello
        1st 2 characters of the String is '01'  -   22Hello
        1st 3 characters of the String is '01'  -   333Hello
        1st 4 characters of the String is '01'  -  4444Hello
    Thank you.
    Best Regards,
    Krishna.

    Hi Krishna ,
    Using IF and MATCH to do that: ( I took one of the conditions you specified )
    IF(match("STRING1",'??0??'),'333Hello',"STRING1")
    Output:
    Regards,
    Krishna Tangudu

Maybe you are looking for