IF-ELSE statement in BI  Query

Hi guys i have a requirement where in i have condition as mention below...
If  say X=0 then Y=0  and
    if    X>0  then y=1
  where X  is  key figure value...to be compared ..
can any one suggest me the logic as to how i need to design this in the query..any option of using IF-ELSE statement...
an in the result if  Y=0 then i should show as a * (star)..how can we do this in the query?
can any one suggest me a solution for this please..
Rgds
Shilpa

shipa,
please check these links
[Defining Exceptions|http://help.sap.com/SAPHELP_NW04S/helpdata/EN/43/21b4cd14cd06f4e10000000a422035/frameset.htm]
[Defining and Changing Exceptions|http://help.sap.com/saphelp_nw04/helpdata/EN/c2/f6843b49f6a40de10000000a11402f/content.htm]

Similar Messages

  • IF ELSE Statement in BEx Query based on the results

    Hi experts,
    I want to realize the following IF/ELSE statement in a BEx Query in respect to the result values
    If the result of KF1 equals 0, than set the KF2 to 0, Else (KF1 NE 0) show the value of KF2
    Background: I want to report the order backlog. In the example above we have an incoming order value of 23.700,80 EUR in periode 12.2013 and an turnover of 22.370,80 EUR in periode 01.2014. The order backlog in ST (KF1) is 1 ST (12.2013) - 1 ST (01.2014) = 0 ST. The order backlog in EUR (KF2) is 23.700,80 EUR (12.2013) - 22.370,80 EUR (01.2014) = 1.330 EUR. The Order and Invoice is finished, but we have an positive order backlog in EUR (reason: less turnover as incoming order).
    For this case I want to realize the IF/Else statement to check if the oder backlog in ST = 0, if yes, than set the order backlog in EUR also to 0.
    Any best practices?
    Many thanks and best regards,
    Michael

    Hi Suman,
    thanks aigain for your reply!
    I have tried your proposal before you have posted it.
    In my special case the following if/else statement works successfully:
    CKF = calculated key figure
    Formula1 = (CKF1 == 0) * 0 + (CKF1 <> 0) * CKF2
    In my case the CKF2 is the result value from "Incoming Order value" - "Turnover"
    So I had to define a separate Formula (1:1 equals to CKF2) and then I was able to set "*CKF2" in the statement.
    The statement CKF2 = (CKF1 == 0) * 0 + CKF2 is not possible (because it´s not possible to calculate from CKF2 itself).
    Now it works
    Best regards,
    Michael

  • How to use IF ELSE statement in one QUERY to MS ACCESS

    System.out.println("Enter Final Exam: ");
    int f = Integer.parseInt(input.readLine());
    command.executeUpdate("UPDATE Students SET Final_Exam='"+f+"',Raw_score=(QT+MT+Final_Exam)/3 WHERE stud_name='"+stdID+"'");
    command.executeUpdate("UPDATE Students SET Raw_score=(QT+MT+Final_Exam)/3 WHERE stud_ID='"+stdID+"'");
    if (rsf==100)
    command.executeUpdate("UPDATE Students SET Final_Grade=1.0 WHERE stud_ID='"+stdID+"'");
    else if(rsf < 100 && rsf > 90)
    command.executeUpdate("UPDATE Students SET Final_Grade=2.0 WHERE stud_ID='"+stdID+"'");
    else
    command.executeUpdate("UPDATE Students SET Final_Grade=5.0 WHERE stud_ID='"+stdID+"'");How can I shorten my code using a IF ELSE statement inside a single query or two? Or is it possible?
    Edited by: ivatanako on Sep 28, 2007 11:55 PM

    Sun has tutorial trail for JDBC, you might want to browse through that; you'll get more details and samples there than here. Here's the link to the section on PreparedStatement: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
    People on the forum help others voluntarily, it's not their job.
    Help them help you.
    Learn how to ask questions first: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
    (Yes I know it's on JavaRanch but I think it applies everywhere)
    ----------------------------------------------------------------

  • IF THEN ELSE STATEMENT IN BeX Query designer

    Hi,
    I want to write a query as follows:
    IF <Value a> is available THEN <Value a> ELSE <Value b>
    I have written it as follows:
    (X>0)X+(X<=0)Y
    The problem with this equation is,  in case X = 0(number Zero) then Y is not getting displayed. But In case X =NULL (or no values) then Y displayed. This is leading to inconsistency in final result. Please help in rectifying the formula incase it is incorrect or is it because of some program bug in BeX?
    Regards
    Madhav

    This works for me...dont understand y its not working with u!!
    it is simple:
    (X<>0)X+(X==0)Y
    If X is 0, then  0X + 1Y = Y
    else
    1X + 0Y = X.
    What result you are getting in both cases?
    as you thing,X value is may not be 0 in your case. It may be 0.00004...something like that...Set Decimal Places to 5 or 6 to X.
    Message was edited by:
            Muralidhar C

  • Charactersitic values to be used in IFTHEN ELSE STATEMENT IN QUERY

    scenario: i need to use the characteristic values(not the attributes) to be used in an IF THEN ELSE STATEMENT in query. so i created a formula variable for the characteritic of processing type replacement path, replace with key.The values are not fetched in the result and is displayed as X.
    what should i replace with key,lable, constant or other options whereas  my requirement is the all the values present in the characteristc should be used.
    eg. if char name is xyz and has the values 1,2,3,4,5 till -
    20.
    then in a calculated key figure :(formulavariable =1)* kefig1keyfig2 +not(formulavariable =1)kef2keyfig3.
    this formula variable should have all the values of the characteristic values ie 1,2,3---20.
    2. how should i do get the values of characterstics values in the equation give me the steps in detail..
    3. i do not want to use the attributes, though i tried initally while creating the formual variable i replaced with attribute value, there is was an error and there were no reult.
    i tried to create char. variable  for the characteristic  of manual entr , not ready for input values and gave all the values 1,2,3 --20 as default values and then created formaul variable and repalce with the variable, however this characteristic variable is not at all displayed from the list of variables which will be displayed while creating the formula variable.
    help me out.
    Thanks

    hi srini,
    i have includedthe char in rows. i want you to explain in detail how to use these values in the if then else statement in query.
    i have created a calculated key figure 2= (formual variable =1 & keyfig a =0)* keyfig1kefig2 +not(formual variable =1 & keyfig a =0)keyfig2 *calculated keyfig1.
    so this formula variable of replacement path have ref. to then char. and with what value should it replace, key, name, external char or attribut etc so that i should get the values of the char from 1 to20.
    i donot to repalce with the attribute. i just want the char. values and not thier attributes (1. description 2. the second attribute has the values say abc1,efg2,ghi3 for respective charteristic values.
    ie
    char value               desc                 2nd attrib
    1                            hi                     abc1
    2                            bye                  defg2
    3                            ciao                  ghi3

  • Looking for a best query for multiple IF Else statement in a single select

    Hi
    I want to run multiple IF Else statements in a single select SQL, each statement is one SQL operating on the same table, what is the best way to write this select SQL query ?
    If it is PL/SQL, when i get the result from the first IF statement I will skip the remaining execution, and so on... Can any one help me on this.
    Thanks in advance !!

    965818 wrote:
    I Apologize, the information i have given might not be enough.
    This is my scenario,
    I am selecting set of rows from the table for the employee id. After selecting those records,
    i need to go through the result list and check the condition 1, if it is met, i will return that employee record.
    If that condition 1 is not met, then i need to go through the condition 2. If that is met, i will return that record.
    Like wise, i have four conditions.
    I am trying to achieve this in a single sql. If i am not clear, please let me know.Not fully clear yet, but the picture is better already. The thing with SQL is that you should stop thinking procedurally. Instead think in data sets.
    For example if the task is:
    Find all managers that work in sales.
    Procedural thinking would work like this:
    pseudo code
    Loop over all employees that work in sales
       for each row
           check if it is a manager
               if manager
                  then return record
               else
                  do nothing
               end
    end loopThinking in datasets will result in a different logic
    pseudo code
    select all employees
    where department = SALES
    and job = MANAGERThis advantage here is that all the "Do nothing" loops are not needed. Those are already eliminated by the database.
    So what is needed to help you? Give the full picture. What is your task that you try to solve. From a business perspective.

  • IF...ELSE.. STATEMENT in BEx Query Designer

    Hi,
    Why there is no IF, ELSE function in BEx Query Designer?
    Assuming I need to write the following logic, how can it be accomplished?
    IF (A=0 OR B=0)
        THEN 0
    ELSE IF ( B/A <0)
        THEN '       *'
    ELSE
        B/A * 100
    i.e.
    if A or B is equal to 0, then display the value of 0
    else if B is negative (as A is either 0 or positive), then display the following text: '         *'
    otherwise, display the value of  B/A * 100
    Thanks!

    Hi,
    You can achieve this by creating a new formula.
    You have to use the boolean operations and mathematical functions. But we cannot display *. u have to display some value for that.
    Khaja

  • IF ELSE statement / CMOD Code in BEX Query

    Hi
    I have a requirement in Inventory flow.
    Material stock value at the end of each quarter has to be calculator based on cycle count indicator. I have all the values on my cube.
    In the report, Calender year and quarter is on the selection screen.
    When user enters 2014 and Q2, Data for April, May and June are displaying on the report. But user want data only for June.
    At the end of quarter 2 is June, so he wants only Material stock value at the end of June.
    If a material stock level is changed in April and May and not changed in June, not data is available for June in my Cube.
    In this case for Quarter 2, if June data is missing, it has to look for May data. Even if MAY data is missing, it will have to look for April.
    Even if April data is missing, it will have to look for March data.
    The result should be
    Plant    Q1    Q2    Q3   Q4
    XYZ     100    100  100    100
    If there is no change to stock level in April, May & June - at the end of Quarter 2, the stock value should be 100 as it was in end of Q1.
    If no change was dont to stock level until December, the stock level should be 100.
    Is there a way out using IF ELSE statements or through CMOD customer exit.
    Regards,
    Elango Murgesan

    HI Elango,
    the user input on CALQuarter always would be Single value??or can we expect ranges as well?
    if single value,
    then we can achieve it by having one more dummy KF.
    lets say. user input is Q2. 
    -> in your dummy KF, restrict the Calquarter/Calmonth to last quarter(i.e Q1)using customer exit and make the logic as you said above(last value based on calmonth).(make this KF hidden, as this is not required to display)
    -> and in another KF restricted with Q2.Same logic, last value on Calmonth.((make this KF hidden, as this is not required to display)
    and now you have 2 KFs in your report.  So in a formula, you can have condition-> if Q2 KF is blank, then Q1KF, else Q2 KF.
    Hope you are getting my idea.. Please try this and let us know.
    ** if Q1 also null, then we can extend customer exit logic to may be last one year, and having last value on based on calendar month, we can still achieve i guess.
    Regards,
    Sakthi.

  • If else statement

    hi everyone
    i need help
    I'm trying to make if else statement in the sqldeveloper query
    and my code us like ...
    IF CHARGE_PRICE BETWEEN 10000 AND 20000
    THEN
    charge_apartment(1,600);
    ELSIF CHARGE_PRICE BETWEEN 20000 AND 40000
    THEN
    charge_apartment(1,500);
    ELSIF CHARGE_PRICE > 40000
    THEN
    charge_apartment(1,900);
    ELSE
    charge_apartment(1,650);
    END IF;
    now the question is how do i cover that part of code so the sqldeveloper query will know that I'm talking about an Apartments table...?
    I dig around the net and so far all the info i got is relevant only for sql*Plus
    so...
    How can I do this at the query?
    thanks in advance
    Edited by: 885780 on Sep 16, 2011 4:04 PM
    Edited by: 885780 on Sep 16, 2011 4:05 PM

    I'll explain myself again more clearly
    if i', execute the command
    execute Charge_Apartment(1,600)
    so the right apartment is being charged with the right peice,
    But!
    if i'm execute
    CASE
    WHEN APARTMENT_SIZE < 10000 THEN Charge_Apartment(1,650);
    WHEN APARTMENT_SIZE < 20000 THEN Charge_Apartment(1,600);
    WHEN APARTMENT_SIZE < 40000 THEN Charge_Apartment(1,500);
    ELSE Charge_Apartment(1,900);
    END
    I'm crashing with
    Error starting at line 1 in command:
    CASE
    Error report:
    Unknown Command
    Error starting at line 2 in command:
    WHEN APARTMENT_SIZE < 10000 THEN Charge_Apartment(1,650)
    Error report:
    Unknown Command
    Error starting at line 3 in command:
    WHEN APARTMENT_SIZE < 20000 THEN Charge_Apartment(1,600)
    Error report:
    Unknown Command
    Error starting at line 4 in command:
    WHEN APARTMENT_SIZE < 40000 THEN Charge_Apartment(1,500)
    Error report:
    Unknown Command
    Error starting at line 5 in command:
    ELSE Charge_Apartment(1,900)
    Error report:
    Unknown Command
    Error starting at line 6 in command:
    END
    Error report:
    Unknown Command
    If someone can help me i'll be glad
    Thanks in advance 4 all of you who helped so far ...

  • IF ELSE statement in BEX

    Hi all,
    I need to write a formula in a query using  IF ELSE statements. Here is my senario
    I need to write a IF ELSE statement to calculate a KF i.e., % variance of This year with last year sales (TY%LY) .Following are the conditions.
    IF LY<0 then TY % LY should be (TY-LY)/LY * (-100)
    IF LY>0 then TY% LY should be (TY-LY)/LY * (100)
    IF LY =0 then TY%LY should be 100
    IF LY=NULL (i.e., if store is closed last year we donu2019t pull record so LY will be empty) then TY%LY should be 100
    IF TY=0 or NULL then TY%LY should be -100
    So I tried writing as follow but getting errors
    (LY<0)(TY-LY)/LY(-100) + (NOT(LY<=0))((TY-LY)/LY)100  + (LY==0)100 + NODIM(LY)100 + (TY==0)(-100) + NODIM(TY)(-100)
    But it is giving errors. Can you please suggest me how to write that formula without effecting the current performance of the query.
    Thanks in Advance,
    Preethi

    Hi Preethi,
    I think that you can accomplish the desired results using the following formulas:
    KEY FIGURE = NOT(F1=0) * F5 + (F1=0) * -100
    F1 = NOERR(TY + 0)
    F2 = NOERR(LY + 0)
    F3 = NOERR( (F1-F2) / F2 ) * -100
    F4 = F3 * -1
    F5 = (F2<0) * F3 + (F2>0) * F4 + (F2=0) * 100
    I hope it helps you.
    Regards,
    Maximiliano

  • If..then..else statement in SQL with 'generalized' conditions in the if sta

    it is possible to write something approaching an if..then..else statement in SQL with 'generalized' conditions in the if statement.
    Attached is the query for the payment register, in which I've written a series of decode statements, one for each possible value of the payment code. The query works OK - however, its specific and as the number of paycodes expand (and they do), the report won't pick up the new paycode until the code is changed. More importantly, the report won't be correct until someone 'discovers' that a paycode is missing, which might take months.
    If I were writing the equivalent of this series of decode statements in Focus, it would be something like this:
    DEFINE.......
    PAYMED/D12.2 = IF PAYMENT_CD LE 18
                   THEN PAYMENT_AMT
                   ELSE 0 ;
    PAYIND/D12.2 = IF PAYMENT_CD GE 19 AND PAYMENT_CD LE 49
                   THEN PAYMENT_AMT
                   ELSE 0 ;
    PAYEXP/D12.2 = IF PAYMENT_CD GE 70
                   THEN PAYMENT_AMT
                   ELSE 0 ;
    PAYREC/D12.2 = IF PAYMENT_CD GE 50 AND PAYMENT_CD LE 69
                   THEN PAYMENT_AMT
                   ELSE 0;
    END
    IN SQL/PLUS:
    SELECT ACCOUNT_NAME,
    LOCATION_1,
    CLMNT_LAST_NAME,
    CLAIM_NBR,
    DATE_OF_INJURY,
    DATE_CHECK_REGISTER,
    PAYEE_NAME_1,
    PAYMENT_CD,
    SERV_OFC,
    CPO_CHECK_NBR,
    PAYMENT_FORM,
    DECODE(PAYMENT_CD, 20, PAYMENT_AMT, 21, PAYMENT_AMT,
    22, PAYMENT_AMT, 23, PAYMENT_AMT, 25, PAYMENT_AMT,
    26, PAYMENT_AMT, 27, PAYMENT_AMT, 28, PAYMENT_AMT,
    29, PAYMENT_AMT, 30, PAYMENT_AMT, 31, PAYMENT_AMT,
    32, PAYMENT_AMT, 33, PAYMENT_AMT, 34, PAYMENT_AMT,
    35, PAYMENT_AMT, 36, PAYMENT_AMT, 37, PAYMENT_AMT,
    39, PAYMENT_AMT, 40, PAYMENT_AMT, 41, PAYMENT_AMT,
    42, PAYMENT_AMT, 43, PAYMENT_AMT, 44, PAYMENT_AMT,
    45, PAYMENT_AMT, 46, PAYMENT_AMT, 47, PAYMENT_AMT,
    48, PAYMENT_AMT, 49, PAYMENT_AMT, NULL) INDEMNITY,
    DECODE(PAYMENT_CD, 0, PAYMENT_AMT, 1, PAYMENT_AMT,
    2, PAYMENT_AMT, 3, PAYMENT_AMT, 4, PAYMENT_AMT,
    5, PAYMENT_AMT, 6, PAYMENT_AMT, 7, PAYMENT_AMT,
    8, PAYMENT_AMT, 9, PAYMENT_AMT, 10, PAYMENT_AMT,
    11, PAYMENT_AMT, 12, PAYMENT_AMT, 13, PAYMENT_AMT,
    14, PAYMENT_AMT, 15, PAYMENT_AMT, 18, PAYMENT_AMT,
    17, PAYMENT_AMT, NULL) MEDICAL,
    DECODE(PAYMENT_CD, 70, PAYMENT_AMT, 71, PAYMENT_AMT,
    72, PAYMENT_AMT, 73, PAYMENT_AMT, 74, PAYMENT_AMT,
    75, PAYMENT_AMT, 76, PAYMENT_AMT, 77, PAYMENT_AMT,
    78, PAYMENT_AMT, 79, PAYMENT_AMT, 80, PAYMENT_AMT,
    81, PAYMENT_AMT, 82, PAYMENT_AMT, 83, PAYMENT_AMT,
    84, PAYMENT_AMT, 85, PAYMENT_AMT, 86, PAYMENT_AMT,
    87, PAYMENT_AMT, 88, PAYMENT_AMT, 89, PAYMENT_AMT,
    90, PAYMENT_AMT, NULL) EXPENSES,
    DECODE(PAYMENT_CD, 50, PAYMENT_AMT, 51, PAYMENT_AMT,
    52, PAYMENT_AMT, 53, PAYMENT_AMT, 54, PAYMENT_AMT,
    55, PAYMENT_AMT, 56, PAYMENT_AMT, 57, PAYMENT_AMT,
    58, PAYMENT_AMT, NULL) RECOVERIES,
    DECODE(PAYMENT_FORM, 'N', PAYMENT_AMT, NULL) NONCASH,
    DATE_FROM_SERVICE,
    DATE_THRU_SERVICE
    FROM &INPUT_TABLES
    WHERE &SECURITYCOND
    DATE_OF_PAYMENT BETWEEN :START_DATE AND :END_DATE
    ORDER BY LOCATION_1, CPO_CHECK_NBR
    As you can see, this is both much easier to write and covers the possibility of expansion of paycodes (expansions always fit in these defined ranges).
    My question is, then, is it possible to write something like this in SQL and, if so, could you give me some sample code? (I'm one of those people who learn best from looking at the code as opposed to a set of instructions)

    Here is one way you could do it.
    Create a table that has columns like:
    Payment_code varchar2(2),
    Effective_Date Date,
    Payment_type varchar2(20),
    Expiration_Date Date)
    Payment type for example could be
    I- indemnity
    M- medical
    R- recovery
    E- expenses
    Let the table name for example be PAYMENT_CODE.
    The select query would look like
    SELECT ACCOUNT_NAME,
    LOCATION_1,
    CLMNT_LAST_NAME,
    CLAIM_NBR,
    DATE_OF_INJURY,
    DATE_CHECK_REGISTER,
    PAYEE_NAME_1,
    PAYMENT_CD,
    SERV_OFC,
    CPO_CHECK_NBR,
    PAYMENT_FORM,
    DECODE(p.payment_type,'E',PAYMENT_AMOUNT,0) expenses,
    DECODE(p.payment_type,'I',PAYMENT_AMOUNT,0) indemnity,
    DECODE(p.payment_type,'M',PAYMENT_AMOUNT,0) Medical,
    DECODE(p.payment_type,'R',PAYMENT_AMOUNT,0) recoveries,
    DECODE(PAYMENT_FORM, 'N', PAYMENT_AMT, NULL) NONCASH
    FROM &INPUT_TABLES,
    PAYMENT_CODE P
    WHERE P.PAYMENT_CODE = SOMEINPUT_TABLE.PAYMENT_CODE
    and other conditions
    The idea is to group all the payment codes into a few groups to reduce the clutter. If there is ever a change to the payment code, you could modify the table and it will be reflected in your select query.

  • How can I use an IF ELSE statement and the xdoxslt: get_variable together?

    First of all, I'm a big fan of this forum and I just want to thank the community for helping make the BI Publisher world a better place.
    I'm trying to populate a table with a years worth of data with the column=PRODUCT and row=month of DATE. Here's the catch, not all of the months will have data. So here's where my problem is, I'm not able to use an IF ELSE statement along with a XDOXSLT:GET_VARIABLE expression so that if there's no data for that month the default value will be 0.
    Can someone help me find a solution to either one of the attempts below or a completely new method? Thanks!
    BP4 = numeric month value using DATE (more code to pull only month from DATE not included here)
    JAN1 = PRICE if data exists, else 0
    1) I tried this and it didn't work:
    <?if: xdoxslt:get_variable($_XDOCTX,'BP4'),01 then xdoxslt:set_variable($_XDOCTX, 'JAN1',PRICE) else xdoxslt:set_variable($_XDOCTX, 'JAN1',0) end if?>
    <?xdoxslt:get_variable($_XDOCTX,'JAN1')?>
    <?end if?>
    2) I tried this and it didn't work:
    <?xdoxslt:ifelse(xdoxslt:get_variable($_XDOCTX,'BP4')='01',xdoxslt:set_variable($_XDOCTX, 'JAN1',PRICE),xdoxslt:set_variable($_XDOCTX, 'JAN1',0))?>
    <?xdoxslt:get_variable($_XDOCTX,'JAN1')?>
    <?end if?>
    3) I tried this and it didn't work:
    <?xdofx:if xdoxslt:get_variable($_XDOCTX,'BP4')='01' then xdoxslt:set_variable($_XDOCTX, 'JAN1',PRICE) else xdoxslt:set_variable($_XDOCTX, 'JAN1',0) end if?>
    <?xdoxslt:get_variable($_XDOCTX,'JAN1')?>
    <?end if?>
    sample XML:
    <ROW>
    <Name>Craig Hernandez</NAME>
    <DATE>2013-01-01T00:00:00.000+08:00</DATE>
    <PRICE>31</PRICE>
    <PRODUCT>BPD</PRODUCT>
    </ROW>
    -DrT
    Edited by: 990965 on Feb 28, 2013 8:27 AM

    let me clarify my logic, the template 1) searches for the relevant PRODUCT (in the case below, it searches for BPD which is the column in the table) 2) searches for the relevant month in DATE (which is the row in the table) 3) if data exists, get PRICE else default to 0
    Variable definitions:
    BP4 = numeric month value from DATE (more code to get this value not included here, but it works...)
    JAN1 = set to PRICE if data exists for that month, else 0
    the query is only pulling all of the existing data, so if there's no data for March then there wouldn't be a March entry in the xml. In the sample xml below, we only have data for the first two months (Jan, Feb) so that's the only data in the xml. I can't check if PRICE is null because the xml will not have any data for March.
    I've read through other threads and it seems that BI Publisher can't use an IF THEN ELSE with the XDOXSLT:GET_VARIABLE. I sent the template and xml to you.
    Sample XML:
    <ROW>
    <Name>Craig Hernandez</NAME>
    <DATE>2013-01-01T00:00:00.000+08:00</DATE>
    <PRICE>31</PRICE>
    <PRODUCT>BPD</PRODUCT>
    </ROW>
    <ROW>
    <Name>Craig Hernandez</NAME>
    <DATE>2013-02-01T00:00:00.000+08:00</DATE>
    <PRICE>30</PRICE>
    <PRODUCT>BPD</PRODUCT>
    </ROW>
    Edited by: 990965 on Feb 28, 2013 8:30 AM

  • Case Statement in sub query

    Hi, I have two issues, here is my initial code:
    select
    cc.name_id_no
    ,cc.discover_date
    ,cc.cla_case_no
    ,max(rl.year_of_incident)Non_Loss_Past_5
    ,rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.
    To get around this I did the following which is not very efficient and I'm hoping it can be improved:
    select distinct z.cla_case_no from (
    select
    cc.name_id_no
    ,cc.discover_date
    ,cc.cla_case_no
    ,max(rl.year_of_incident)Non_MW_Loss_Past_5
    ,rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
    SELECT
    ie ,(select distinct z.cla_case_no from (
    select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    where z.cla_case_no = ccx.cla_case_no
    ) Non_MW_Loss_Past_5
    FROM etc
    Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
    What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
    Thanks!!!
    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

    Hi,
    It looks like you have another copy of this question:
    Case Statement and sub query
    That's probably not your fault, but you should mark the other copy as "Answered" right away, and then you'll only have to look for replies in one place.
    885178 wrote:
    ... Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.If you know there will only be one, then you can use LAST, and you don't need GrOUP BY
    To get around this I did the following which is not very efficient and I'm hoping it can be improved:
    select distinct z.cla_case_no from (
    select
    cc.name_id_no
    ,cc.discover_date
    ,cc.cla_case_no
    ,max(rl.year_of_incident)Non_MW_Loss_Past_5
    ,rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) zHere's one way:
    SELECT       MIN (cla_case_no) KEEP (DENSE_RANK LAST ORDER BY r1.year_of_incident)
                         AS latest_cla_case_no
    FROM       cla_case     cc
    ,             rbn_loss      rl
    WHERE     cc.name_id_no          = rl.customer_no
    AND       rl.year_of_incident     > TRUNC (cc.discover_date) - 1095
    AND       rl.year_of_incident      < TRUNC (cc.discover_date)
    AND       rl.type_of_loss     < 1000
    AND       rl.timestamp          < TRUNC (cc.discover_date)
    AND       cc.question_class     IN (20, 25)
    ;If you'd post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data, then I could test this.
    Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
    SELECT
    ie ,(select distinct z.cla_case_no from (
    select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    where z.cla_case_no = ccx.cla_case_no
    ) Non_MW_Loss_Past_5
    FROM etc
    Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
    What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
    NVL2 (x, 'Y', 'N')returns 'Y' if x is NULL, and it returns 'N' if x is not NULL. X can be a scalar sub-query:
    NVL2 ((SELECT ...), 'Y', 'N')You could also use an EXISTS sub-query:
    CASE
        WHEN  EXISTS (SELECT ...)
        THEN  'Y'
        ELSE  'N'
    END

  • Invalid state in SQL query for a function that was created with no errors.

    SQL> CREATE OR REPLACE FUNCTION overlap(in_start1 IN TIMESTAMP, in_end1 IN TIMESTAMP, in_start2 IN TIMESTAMP, in_end2 IN TIMESTAMP) RETURN NUMBER
    2 IS
    3
    4 BEGIN
    5 IF (in_start1 BETWEEN in_start2 AND in_end2 OR in_end1 BETWEEN in_start2 AND in_end2 OR in_start2 BETWEEN in_start1 AND in_end1) THEN
    6 RETURN 0;
    7 ELSE
    8 RETURN 1;
    9 END IF;
    10 END;
    11 /
    Function created.
    SQL> show errors;
    No errors.
    SQL>
    SQL> SELECT * FROM tbl where overlaps(current_time,current_time+1,current_time-1,current_time+2) = 0;
    SELECT * FROM tbl where overlaps(current_time,current_time+1,current_time-1,current_time+2) = 0
    ERROR at line 1:
    ORA-06575: Package or function OVERLAPS is in an invalid state
    I do not understand why overlaps is returned as in invalid state in the query, when it was created with no errors earlier. Could anyone help me?

    Marius
    Looking at the logic you are trying to create it looks like you are looking for overlapping time periods.
    Consider two date/time ranges:
    Range 1 : T1 - T2
    Range 2 : T3 - T4
    Do they overlap?
    1) No: T1 < T4 (TRUE)  T2 > T3 (FALSE)
    T1 --- T2
               T3 --- T4
    2) Yes: T1 < T4 (TRUE)  T2 > T3 (TRUE)
    T1 ---------- T2
               T3 --- T4
    3) Yes: T1 < T4 (TRUE)  T2 > T3 (TRUE)
    T1 -------------------- T2
               T3 --- T4
    4) Yes: T1 < T4 (TRUE)  T2 > T3 (TRUE)
                   T1 ----- T2
               T3 --- T4
    5) Yes: T1 < T4 (TRUE)  T2 > T3 (TRUE)
               T1 --- T2
           T3 ------------ T4
    5) No: T1 < T4 (FALSE) T2 > T3 (TRUE)
                    T1 --- T2
           T3 --- T4Answer: Yes they overlap if:
    T1 < T4 AND T2 > T3
    So you can code the logic in your SQL as simply:
    SELECT *
    FROM tbl
    WHERE range1_start < range2_end
    AND    range_1_end > range2_startIf you go around implementing PL/SQL functions for simple logic that can be achieved in SQL alone then you cause context switching between the SQL and PL/SQL engines which degrades performance. Wherever possible stick to just SQL and only use PL/SQL if absolutely necessary.

  • Jdbc driver creates new thread for each statement with a query timeout set

    I am profiling a web application that is using the Microsoft JDBC driver, version 1.1 to connect to a sql server 2005 database. Each java.sql.Statement that is created, within the application, gets a query timeout value set on it ( statement.setQueryTimeout(...) ).
    I have discovered that the JDBC driver creates a new thread to monitor each Statement and the query timeout value. When the application is under load these threads are getting created faster then they are being destroyed and I am concerned that this will cause a performance problem in production.
    One option I have is to remove the query timeout value and the monitor threads will not be created, another is to change JDBC drivers.
    I'm curious is there any way to control this behavior so that these threads are not created or are managed more efficiently.  Is there a workaround that anyone is aware of?   Is this considered a bug?
    I have found a similar bug here for the 2000 driver:
    http://support.microsoft.com/default.aspx/kb/894552
    Cheers

    Hi,
    Thank you for using the Microsoft SQL Server JDBC driver.  You are correct that a new thread is started to monitor the query timeout when a statement is executed.  This is a potential performance issue that we may look at addressing in the upcoming v1.2 release.  The current behavior is not configurable.  The only workaround with the v1.1 driver is to limit your use of query timeouts to only those statements which you reasonably might expect to actually time out.
    We do take customer feedback very seriously, so if anyone else is concerned about this particular issue, please chime in and we will be able to give it appropriate weight.
    --David Olix [MSFT]

Maybe you are looking for