Case statement in where clause

Hello

I have to write the following query using the value of the l_var variable in the case statement

How do I get there?


Select col3

of the test

where col1 = "A".

and the case

When l_var = "Y' and col2 = 'B '.

When l_var = 'n' and col2 = 'C '.

end;

In your expression

case

When l_var = 'Y' and col2 = 'B '.

When l_var = ' only then col2 <> 'B '.

end;

you try to return a Boolean (True or False) value like 'then' value of the CASE statement.

Values Boolean are not an Oracle SQL data type, you cannot use the true/false result of col2 = 'B' as an expression. This is why it does not work.

It's even more simple as:

Select col3

of the test

where col1 = "A".

and ((l_var = «Y» et col2 = «B») or (l_var = ' no and col2 <> 'B'))

If you really want a CASE statement then:

Select col3

of the test

where col1 = "A".

and the CASE WHEN l_var = 'Y', THEN CASE WHEN col2 = 'B', 1 ELSE 0 END

WHEN l_var = ' no CASE THEN WHEN col2 <> 'B', 1 ELSE 0 END

END = 1

Tags: Database

Similar Questions

  • Using the Case statement in Where clause with operator

    Hi all

    I'm doing the following work (this is just a simple version of what I'll eventually need):

    Sorry, I forgot how to use the code tags...

    and li. MAJOR_ACCT in case
    When: prompt = 'Energy' then ('9320906 ', ' 9321471')
    end


    Problem is that I get a missing closing parenthesis error.

    Wouldn't - that evaluate against a value(as below) it works fine:

    and li. MAJOR_ACCT in case
    When: prompt = 'Energy' then ('9320906')
    end


    Any thoughts? Is it possible to use a box (or decode) in an In clause with multiple values? I tried to use decode as well and get the same results.

    Thank you.

    Darren.

    What:

    and ( case
            when :prompt = 'Energy' and li.MAJOR_ACCT in ('9320906', '9321471') then 1
            when :prompt = 'Vehicle' and li.MAJOR_ACCT in ('9812180', '9812320')  then 1
            when :prompt = 'Meals' and li.MAJOR_ACCT in ('983120', '983452') then 1
          end
        ) = 1
    
  • CASE in a WHERE clause: ORA-00905

    Hello world
    I try to use a BOX in a WHERE clause clause and I got the error ORA-00905: lack of keyword. Here is my code:

    SELECT id_reserv,
    Concat (name, Concat ('_', index)) as name,
    Libelle,
    num_lot,
    resa_keyword1,
    resa_keyword2,
    resa_keyword3,
    resa_keyword4,
    date_creation,
    comm_creation,
    id_util,
    assets,
    id_env_act,
    (SELECT connection of user u where u.id_util = r.id_util) AS nom_util,
    (SELECT name e environment where e.id_env = r.id_env_act) AS nom_env,
    (SELECT count (*) of rc reserv_comp where rc.id_reserv = r.id_reserv) AS nbComp,
    (SELECT count (*) MC reserv_modif where mc.id_reserv = r.id_reserv) AS nbModif
    BOOKING r
    WHERE the r.nom NOT LIKE 'RESERV_LOT_ % '.
    AND id_util = '1'
    AND active = '1'
    AND id_env_act > = '-1'.
    AND the MATTER sansdemande
    WHEN true THEN id_reserv not in some id_reserv from demande_livraison where id_env_dep > = '-1'.
    ELSE id_reserv = id_reserv
    END
    AND name LIKE '% '.
    ORDER BY date_creation;

    I already looked at the CASE statement and it seems that the syntax is correct, so I don't know I can use in a WHERE clause.
    Any help would be nice!

    Andalusians

    Hello

    It should be something like this:

    AND CASE
      WHEN 'false'='true' THEN (select id_reserv from demande_livraison where id_env_dep>='-1')
      ELSE id_reserv
         END = id_reserv 
    

    The subquery must return a line

    But I think that it is better to write your query in the form:

    SELECT id_reserv,
         concat(nom,concat('_',indice)) as nom,
         libelle,
         num_lot,
         resa_keyword1,
         resa_keyword2,
         resa_keyword3,
         resa_keyword4,
         date_creation,
         comm_creation,
         id_util,
         actif,
         id_env_act,
         (SELECT login from utilisateur u where u.id_util=r.id_util) AS nom_util,
         (SELECT nom from environnement e where e.id_env=r.id_env_act) AS nom_env,
         (SELECT count(*) from reserv_comp rc where rc.id_reserv=r.id_reserv) AS nbComp,
         (SELECT count(*) from reserv_modif mc where mc.id_reserv=r.id_reserv) AS nbModif
    FROM reservation r
         WHERE r.nom NOT LIKE 'RESERV_LOT_%'
         AND id_util='1'
         AND actif='1'
         AND id_env_act>='-1'
         AND
                       (
                          (  'false'='true' and id_reserv not in (select id_reserv from demande_livraison where id_env_dep>='-1')
                          )
                          or
                          ( 'true'= 'true' and id_reserv=id_reserv
                          )
                       )
         AND nom LIKE '%'
    ORDER BY date_creation;
    

    This coding in SQL something as if a = b, then c, d also in the simplest form.

    Herald tiomela
    http://htendam.WordPress.com

  • Once again... Help... CASE in the WHERE clause...!

    Hi all

    I posted the question about CASES where the condition. It is one of the changes.

    The first condition was, if the Individual is 01 extraction then all the values of the table where the column of FLG = 'Y '. If the Individual is 02 and then extract all the values of the table where the column of FLG = ' no or NULL. Here, INDI is the setting.

    Select count (*) from cntl
    XYZ
    where NVL(FLG,'N') = decode(INDI,'01','Y','02','N');

    Now the chnages is, if the Individual is 01 and then extract all the values in the table where FLG colum = 'Y '. If the Individual is 02 and then extract all the values in the table, including ('Y', ' n, NULL). no need to put the filter of the FLG columns in which contion.

    It's not just...

    select count(*) into cntl
    from xyz
    where NVL(FLG,'N') = decode(INDI,'01','Y','02',NVL(FLG,'N'));
    

    Or shorter:

    select count(*) into cntl
    from xyz
    where NVL(FLG,'N') = decode(INDI,'01','Y',NVL(FLG,'N'));
    

    If you are happy for 01 retreat flg of Y and any other value fold everything.

    Published by: BluShadow on June 3, 2009 12:31

  • Help... CASE in the WHERE clause...!

    Hi all

    I need help in the following condition. I am passing the parameter say INDI. the value of INDI can be 01 or 02

    I am retriving count in a table. say,

    DECLARE
    CNTL varchar2 (5);
    BEGIN

    Select count (*) from cntl
    XYZ
    where FLG = CASE WHEN INDI = '01', then 'Y' END;

    dbms_output.put_line (cntl);
    END;

    the FLG column can contain the value "Y" or "n" or NULL. If I run the above code, it gives me a count for these lines where FLG = 'Y' for INDI 01. But if I spend INDI = 02, it should give me account for these lines that has FLG = ' not and NULL.

    If I say where FLG = CASE WHEN INDI = '01' and then 'Y' ELSE ' no END; then he'll give me an account for these lines that has FLG = ' don't, but here I want County to FLG = 'n' and FLG = NULL, if I spend INDI = 02

    I am not able to determine how I can achieve this...
    select count(*) into cntl
      from xyz
     where NVL(FLG,'N') = CASE WHEN INDI = '01' THEN 'Y'
                         WHEN INDI = '02' THEN 'N'
                     END;
    
  • using case when statement or decode unfavorable in where clause

    Hi the gems...

    I have a problem in the following query...
    I try to use when case statement in where clause of a select query.


    Select cr.customer_name. ' - ' || CR.customer_number as cust_name,
    CR. Salary salary
    of customer_details cr
    where (case when ' > ' = ' > ' then ' cr.salary > 5000')
    When ' > ' = ' < ' then ' cr.salary < 5000'
    When ' > ' = '=' and then 'cr.salary = 5000'
    Another null
    (end);


    the expression in the when clause of the statement of the case, when comes from HQ and according to the choice I have to make the where clause.
    That is why, for the execution of the query, I put ' > ' in this place.

    If the original query will look like this (for reference):

    Select cr.customer_name. ' - ' || CR.customer_number as cust_name,
    CR. Salary salary
    of customer_details cr
    where (case when variable = ' > ' then ' cr.salary > 5000')
    When the variable = ' < ' then ' cr.salary < 5000'
    When the variable = '=' and then 'cr.salary = 5000'
    Another null
    (end);


    so, in a real case, if the user selects ' > ' then the filter will be ' where cr.salary > 5000.
    If the user selects ' < ' then the filter will be ' where cr.salary < 5000.
    If the user selects '=', then the filter will be 'where cr.salary = 5000 '.

    but I get the error "ORA 00920:invalid relational operator.

    Help, please... Thanks in advance...

    Hello

    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
           cr.salary                                      as salary
     from customer_details cr
    where (    v_variable = 'bigger'
           and cr.salary > 5000
          )
       or (    v_variable = 'less'
          and cr.salary < 5000
           )
       or (    v_variable = 'eq'
            and cr.salary = 5000
           )
    

    Published by: user6806750 on 22.12.2011 14:56
    For some reason that I can't write in sql "<', '="">", "=".

  • Case statement in a WHERE clause

    Gurus,

    Im trying to avoid Union of several select statements using a CASE inside a WHERE clause. When the parameter is defined as "What's new" it is advisable to use a code and for "Update Items" another condition.

    Having a problem with the following... I know that BOX in WHEREs instructions are allowed only can not make it work for this one.

    and case  when 'New Items' = 'xxx' --p_item_status 
             then  msi.creation_date = msi.last_update_date
          when 'Updated Items' = 'bbb' --p_item_status  
             then  msi.creation_date != msi.last_update_date
          else 1=1
    end;  
    Points for correct and helpful answers!

    Published by: sreese on March 27, 2012 17:51

    Hello

    The great thing about CASE expressions, is that they allow you to use a login IF-THEN-ELSE in the clause SELECT, the ORDER BY clause or anywhere else. The WHERE clause has its own way to IF-THEN-ELSE logic, so even if you can use a CASE expression in a clause WHERRE, it usually does not help anything. You can do somehting like this:

    WHERE     (     :p_item_status        = 'New Items'
         AND     msi.creation_date  = msi.last_update_date
         )
    OR     (     :p_item_status        = 'Updated Items'
         AND     msi.creation_date  != msi.last_update_date
         )
    OR     (     :p_item_status        NOT IN ( 'New Items'
                               , 'Updated Items'
                               )
         )
    

    This guess: p_item_status is not NULL.
    If: p_item_status can be NULL, and you want to include all the lines where it is, and then change the last condition to

    OR     ( NVL (:p_item_status, 'OK') NOT IN ( 'New Items'
                                 , 'Updated Items'
                                 )
         )
    

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions), some values of the parameter and the expected results of these data for each value of the parameter.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

  • Help with making SQL query references to column aliases in the Case statement

    I need help with a sql query that I'm trying. I can go about it the wrong way, but I would be grateful if I could get any suggestions on possible solutions. This is my query:


    SELECT DISTINCT spriden_pidm, spriden_id id, spriden_last_name | ',' | spriden_first_name name,

    CASE
    WHEN rcresar_comm_code_01 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_01
    WHEN rcresar_comm_code_02 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_02
    WHEN rcresar_comm_code_03 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_03
    WHEN rcresar_comm_code_04 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_04
    WHEN rcresar_comm_code_05 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_05
    WHEN rcresar_comm_code_06 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_06
    WHEN rcresar_comm_code_07 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_07
    WHEN rcresar_comm_code_08 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_08
    WHEN rcresar_comm_code_09 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_09
    WHEN rcresar_comm_code_10 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_10
    END acg_elig_comm_code

    CASE
    WHEN acg_elig_comm_code = ' 268' THEN 'rigorous HS course. "
    WHEN acg_elig_comm_code = '269' THEN ' 2 or several AP or IB"
    WHEN acg_elig_comm_code = '270' THEN 'NOC as possible ".
    END comm_code_description

    OF spriden, rcresar, rcrapp1

    WHERE (rcresar_comm_code_01 IN ('268 ', '269', ' 270')

    OR rcresar_comm_code_02 ('268 ', '269', ' 270')

    OR rcresar_comm_code_03 ('268 ', '269', ' 270')

    OR rcresar_comm_code_04 ('268 ', '269', ' 270')

    OR rcresar_comm_code_05 ('268 ', '269', ' 270')

    OR rcresar_comm_code_06 ('268 ', '269', ' 270')

    OR rcresar_comm_code_07 ('268 ', '269', ' 270')

    OR rcresar_comm_code_08 ('268 ', '269', ' 270')

    OR rcresar_comm_code_09 ('268 ', '269', ' 270')

    OR rcresar_comm_code_10 ('268 ', '269', ' 270'))


    Rcresar_aidy_code = & aidy_code

    AND rcrapp1_aidy_code = rcresar_aidy_code

    AND rcrapp1_curr_rec_ind = 'Y '.

    AND rcrapp1_seq_no = rcresar_seq_no


    AND spriden_pidm = rcresar_pidm

    AND rcrapp1_pidm = rcresar_pidm


    AND spriden_change_ind IS NULL

    ORDER BY name


    The second case statement is where I don't know exactly what it takes to get what I want.

    Output should be like:
    spriden_pidm name ID acg_elig_comm_code comm_code_description
    «0000000000', ' 1111111111 ","John Doe","268", «rigorous HS race"»

    If I take the second case statement it works great except that I do not have my comm_code description column. My question is how can I use my first statement value box to determine this column? I think that I need a case statement as I have, but I don't know how to reference the value of acg_elig_comm_code. Any help would be greatly appreciated. Thank you.

    Published by: blackhole82 on January 20, 2009 09:20

    Hello

    You cannot use the alias column in the query, even where it is set (except in the ORDER BY clause).
    You can set the alias in a subquery and then use it in a great query, like this:

    WITH  sub_q  AS
    (
        SELECT DISTINCT spriden_pidm,spriden_id id, spriden_last_name||', '||spriden_first_name name,
            CASE
                WHEN rcresar_comm_code_01 IN ('268','269','270') THEN rcresar_comm_code_01
                WHEN rcresar_comm_code_02 IN ('268','269','270') THEN rcresar_comm_code_02
                WHEN rcresar_comm_code_03 IN ('268','269','270') THEN rcresar_comm_code_03
                WHEN rcresar_comm_code_04 IN ('268','269','270') THEN rcresar_comm_code_04
                WHEN rcresar_comm_code_05 IN ('268','269','270') THEN rcresar_comm_code_05
                WHEN rcresar_comm_code_06 IN ('268','269','270') THEN rcresar_comm_code_06
                WHEN rcresar_comm_code_07 IN ('268','269','270') THEN rcresar_comm_code_07
                WHEN rcresar_comm_code_08 IN ('268','269','270') THEN rcresar_comm_code_08
                WHEN rcresar_comm_code_09 IN ('268','269','270') THEN rcresar_comm_code_09
                WHEN rcresar_comm_code_10 IN ('268','269','270') THEN rcresar_comm_code_10
            END acg_elig_comm_code   -- Originally posted with , here (error)
        FROM spriden, rcresar, rcrapp1
        WHERE (rcresar_comm_code_01 IN ('268','269','270')
                OR rcresar_comm_code_02 IN ('268','269','270')
                OR rcresar_comm_code_03 IN ('268','269','270')
                OR rcresar_comm_code_04 IN ('268','269','270')
                OR rcresar_comm_code_05 IN ('268','269','270')
                OR rcresar_comm_code_06 IN ('268','269','270')
                OR rcresar_comm_code_07 IN ('268','269','270')
                OR rcresar_comm_code_08 IN ('268','269','270')
                OR rcresar_comm_code_09 IN ('268','269','270')
                OR rcresar_comm_code_10 IN ('268','269','270'))
        AND rcresar_aidy_code = &aidy_code
        AND rcrapp1_aidy_code = rcresar_aidy_code
        AND rcrapp1_curr_rec_ind = 'Y'
        AND rcrapp1_seq_no = rcresar_seq_no
        AND spriden_pidm = rcresar_pidm
        AND rcrapp1_pidm = rcresar_pidm
        AND spriden_change_ind IS NULL
    )
    SELECT    sub_q.*,
              CASE
                  WHEN acg_elig_comm_code = '268' THEN 'Rigorous HS course'
                  WHEN acg_elig_comm_code = '269' THEN '2 or more AP or IB'
                  WHEN acg_elig_comm_code = '270' THEN 'ACG possible'
              END comm_code_description
    FROM      sub_q
    ORDER BY  name
    

    Furthermore, you might think to rearrange your table, so that you do not have 10 columns (rcresar_comm_code_01, rcresar_comm_code_02,...) that essentially do the same thing. The usual way to handle this kind of one-to-many relationship is to have all rcresar_comm_codes in a separate table, one per line, with a pointer to the table where you have them now.

    Published by: Frank Kulash, January 20, 2009 11:35
    Syntax error has been corrected

  • Tuning - using the CASE statement in the WHERE clause of the query

    Hi all

    My request has been changed to use a CASE statement in the WHERE clause to examine the data to some columns based on a parameter value. This modified request is made a full table and constantly running scan. Please suggest what can be done to improve its performance:

    Query:
    SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
    os.acctnum,
    os.avieworigin_refid,
    COUNT(1) cnt_articleview,
    SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
    FROM TABLE1 os
    WHERE os.acctnum IS NOT NULL
    AND os.avieworigin_refid IS NOT NULL
    AND os.requestdatetime IS NOT NULL
    AND UPPER(os.success_ind) = 'S'
    
    AND CASE WHEN
    Param_ValueToCheck  = 'FULL' AND get_date_timestamp(os.requestdatetime)  BETWEEN 
    TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
    TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    THEN 1
    WHEN
    Param_ValueToCheck  = 'INCR' AND os.entry_createddate  BETWEEN 
    TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
    TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    THEN 1
    END = 1
    AND CASE WHEN
    Param_ValueToCheck  = 'FULL' AND os.entry_CreatedDate BETWEEN 
    TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
    TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
    THEN 1
    WHEN
    Param_ValueToCheck  = 'INCR' THEN 1
    END = 1
    GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;
    Description of the table:
    (Number of lines: approx. > amount 600 000 000)
    Name                           Null     Type       
    ------------------------------ -------- ------------
    ARTICLEID                      NOT NULL NUMBER(20) 
    USERKEY                                 NUMBER(10) 
    AVIEWORIGIN_REFID                       VARCHAR2(10)
    SUCCESS_IND                             VARCHAR2(2)
    ENTRY_CREATEDDATE                       DATE       
    CREATED_BY                              VARCHAR2(10)
    FILENUMBER                              NUMBER(10) 
    LINENUMBER                              NUMBER(10) 
    ACCTNUM                                 VARCHAR2(10)
    AUTOCOMPLETEDTERM                       NUMBER(2)  
    REQUESTDATETIME                         VARCHAR2(19)
    Explain Plan
    SQL> select * from table(dbms_xplan.display);
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 2224314832
    
    ----------------------------------------------------------------------------------------------------------
    | Id  | Operation            | Name              | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
    ----------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |                   |   590 | 33040 |  2501K  (1)| 08:20:15 |       |       |
    |   1 |  HASH GROUP BY       |                   |   590 | 33040 |  2501K  (1)| 08:20:15 |       |       |
    |   2 |   PARTITION RANGE ALL|                   |   590 | 33040 |  2501K  (1)| 08:20:15 |     1 |1048575|
    |*  3 |    TABLE ACCESS FULL | TABLE1 |   590 | 33040 |  2501K  (1)| 08:20:15 |     1 |1048575|
    ----------------------------------------------------------------------------------------------------------
    
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       3 - filter(UPPER("OS"."SUCCESS_IND")='S' AND CASE  WHEN ('FULL'='FULL' AND
                  "OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  "OS"."ENTRY_CREATEDDATE"<=TO_DATE(' 2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN
                  'FULL'='INCR' THEN 1 END =1 AND "OS"."REQUESTDATETIME" IS NOT NULL AND CASE  WHEN ('FULL'='FULL'
                  AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd
                  hh24:mi:ss') AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00',
                  'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN ('FULL'='INCR' AND "OS"."ENTRY_CREATEDDATE">=TO_DATE('
                  2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 END =1 AND "OS"."ACCTNUM" IS NOT NULL AND
                  "OS"."AVIEWORIGIN_REFID" IS NOT NULL)
    Published by: Chaitanya on June 9, 2011 02:44

    Published by: Chaitanya on June 9, 2011 02:47

    When it is executed individually, MERGE LOGIC 1 and LOGIC of MERGE 2 take about ten for a daterange of 30 days data.

    BUT FULL is to get the scores of APR - JUN, INCR becomes partitions for MAY so not both 30 days.
    Are compare us like with like?

  • Using a CASE statement in 'IN' where clause clause

    Hello

    I have a form that users see after they connect with a few items and they type or select values based on the element type. All reports in the application use these values of PAGE 1 in where clause to filter the lines.

    One of these question is "Quarter".  I have a group of radio buttons on the PAGE 1 on the P1_QTR point.

    I need to provide users a way to take the last 4, last 3, last 2 or current quarter and I need to dynamically build a clause WHERE the interactive report based on the selection in the form.

    I am trying to use the CASE as shown below and get the syntax error.

    AND QTR to (when BOX: P1_QTR = 'CURRENT' then ("T4"))

    When: P1_QTR = "LAST" then ("Q3")

    When: P1_QTR = 'LAST TWO' then ('Q3, "Q4")

    When: P1_QTR = 'LAST THREE' then ("Q2", "Q3,' T4 ')

    another ("T1", "T2", "Q3,' T4 ')

    END)

    How can I achieve this?

    Following a simple logic, try

    and QTR to

    (

    Select 'T4' double where: P1_QTR <> 'LAST '.

    Union of all the

    Select 'Q3' to double where: P1_QTR <> "in PROCESS".

    Union of all the

    Select "Q2" double where: P1_QTR not in ('CURRENT', 'LAST', "LAST TWO")

    Union of all the

    Select "Q1" double where: P1_QTR not in ('CURRENT', 'LAST', 'LAST TWO', 'THREE LATEST')

    )

  • case in PL/SQL where clause statement

    • Hello
    • I have a research procedure where the user can search on the city, street, zip code.
    • I need meet the 6 different combinations as I don't know what value the user will pass.
    • It should be checked if the user passes the value or not, and then look on values passed.
    • But the procedure below is out.it the slightest mistake can be transformed with if-then-else, but case statement seems much cleaner in the code.
    • procedure searchaddress (pc_town IN SEARCHADDRESS. CITY % TYPE,

    pc_street IN SEARCHADDRESS. TYPICAL STREET %,

    pc_postcode IN SEARCHADDRESS. POSTCODE % TYPE,

    pResultSet to ref_cursor) as

    Start

    Open the pResultset for

    Select searchaddress_pk from searchaddress

    where

    case

    When pc_town is null then ((street = pc_street) and (ZipCode = pc_postcode))

    When pc_street is null then ((town = pc_town) and (ZipCode = pc_postcode))

    When pc_postcode is null then ((town = pc_town) and (street = pc_street))

    When pc_town is null and pc_street is null then (postal code = pc_postcode)

    When pc_street is null and pc_postcode is null then (city = pc_town)

    When pc_town is null and pc_postcode is null then (street = pc_street)

    end

    end searchaddress;

    Thank you

    CenterB

    A basic concept that is important to understand - sliders aren't 'sets of results' (sets of data in memory). A cursor is a series of executable steps (see plans run to cursor). It's like a program. An extraction run the program and output data. In general, which is repeated until the cursor (aka program) is no longer found the corresponding lines.

    A ref cursor is a pointer or a handle for a such slider, passed to a caller, allowing the appellant to interface directly with this slider/program.

    The slider more effective to create a search would be one without superfluous code and predicates and filters. Consider the following approach - a beefier PL/SQL procedure, do not try and play little tricks with SQL predicates in an attempt to create one (and probably not optimal) SQL.

    (not tested/compiled code)

    create or replace procedure SearchAddress(
      town     IN SEARCHADDRESS.TOWN%TYPE,
      street   IN SEARCHADDRESS.STREET%TYPE,
      postcode IN SEARCHADDRESS.POSTCODE%TYPE,
      refCur out ref_cursor
    ) is
      curHandle integer;
      res integer;
      searchSQL varchar2(1000);
    begin
      -- create base query
      searchSQL := 'select searchaddress_pk from searchaddress where 1 = 1 ';
    
      -- add dynamic predicates as needed
      if town is not null then
        searchSQL := searchSQL || 'and town = :town';
      end if;
    
      if street is not null then
        searchSQL := searchSQL || ' and street = :street';
      end if;
    
      if postcode is not null then
        searchSQL := searchSQL || ' and postcode = :postcode';
      end if;
    
      -- create a DBMS_SQL cursor
      curHandle := DBMS_SQL.open_cursor;
      DBMS_SQL.parse( curHandle, searchSQL, DBMS_SQL.NATIVE );
    
       -- bind the dynamic bind variables added as predicate values
      if town is not null then
        DBMS_SQL.Bind_Variable( curHandle, 'town', town );
      end if;
    
      if street is not null then
        DBMS_SQL.Bind_Variable( curHandle, 'street', street );
      end if;
    
      if postcode is not null then
        DBMS_SQL.Bind_Variable( curHandle, 'postcode', postcode );
      end if;
    
      -- execute cursor (to create the cursor program) and pass it back as a ref cursor
      res := DBMS_SQL.Execute( curHandle );
      refCur := DBMS_SQL.To_RefCursor( curHandle );
    end;
    
  • A CASE statement with additional where clause

    Hi all

    I need assistance in which this obligation clause

    Table:-balance_table

    Columns:-balance_type, balance_amount, balance_month, budget_name

    balance_type column had given 'Real' OR 'Budget '.

    Now I am trying to extract data such as: if: entered_month is Mar-2009 then before and including Mar-2009 will show real balance sheet Date and after Mar-2009 will show budget data.

    It works fine, I need to add a condition more restrict the budget name, there are several budget_name in the table for balance_type = 'Budget '.



    SELECT SUM (balance_amount), balance_type, balance_month
    OF balance_table
    WHERE balance_type =
    (CASE
    WHEN balance_month < =: entered_month
    THEN 'real '.
    ANOTHER 'Budget '.
    END
    )
    AND budget_name = 'BUDGET1.
    Balance_type GROUP, balance_month

    Stated above is erroneous because ' AND budget_name = ' BUDGET1 "clause restricts the set of data."

    Please help in contrcuting where clause 1) to sort the data according to Budget/actual and 2) for specific budgets, so budget_type = Budget

    Thank you
    Bobin

    That should do it.

    SELECT SUM (balance_amount),
           balance_type,
           balance_month
    FROM balance_table
    WHERE (balance_type = 'Actual'
           and balance_month <= :entered_month
          )
    or (balance_type = 'Budget'
        and balance_month > :entered_month
        and budget_name = :entered_budget_name
       )
    GROUP BY balance_type,
             balance_month
    ORDER BY 2
    

    See you soon
    Sarma.

  • Case instructions within a Where clause clause

    Hello group,

    I know this has been asked several times, but I do not enter simply to a tuition assistance box in a WHERE clause.  So I need help:

    My current WHERE the clause reads:

    A16. FULL_DATE between (SELECT (TRUNC (SysDate - 8)) From Dual) and (select (TRUNC (SysDate - 2)) double)

    However, I need to 'automate' a bit, based on the current time/day of the month.  I need my instruction box to say:

    (Case when to_char (sysdate-3, 'mm') <>to_char (sysdate, 'mm')

    then the a16. FULL_DATE (between SELECT (trunc (ADD_MONTHS ((LAST_DAY(SysDate-3)),-1)+1) and (SELECT (add_months (trunc(SysDate-2)-1)) of double)))

    Of another a16. FULL_DATE between (SELECT trunc (ADD_MONTHS ((LAST_DAY (SysDate)),-1)+1) From Dual) and (select (TRUNC (SysDate - 2)) From Dual)

    However, I have an error on "Else".  Can someone explain what is the problem and how to fix it?  I tried both 'then' and 'Else' syntax and both will run individually.  So I'm quite sure that he does not like my CASE statement in general.

    In advance, thank you for your help.

    Don

    I can't tell what your business logic, so here's a simple example of an instruction box in the where clause:

    SQL > select *.
    2 double
    3 where dummy = case when extracted (sysdate months) = 9 then 'W '.
    4 Once extracted (sysdate months) = 10 then 'X '.
    When 5 extract (month from sysdate) = 11 then 'Y '.
    6                      else 'Z'
    7                 end
    8;

    D
    -
    X

  • How to set a null column in a where clause clause involving a statement box

    In oracle APEX, I a (selection list) element called: P32_STATUS. It contains two values of interest (for this discussion): 'Available' and 'checked '. In my report, I have a column called vc_status that contains two values: "Checkbox" and NULL. In my application I want to use a case statement in my where clause similar to the lines of

    where

    .

    .

    .

    AND (vc_status = CASE WHEN: P32_STATUS = "Checkbox" THEN : P32_STATUS )

    WHEN : P32_STATUS = 'Available' THEN NULL END)

    The 'box' filtering works but the filtering to try to vc_status the NULL value does not work. Can someone help me? Thank you!

    (P.S. My intuition says that there because I can not put vc_status = NULL, but I must say that vc_status IS NULL... However, I don't know how to do execution IS ZERO inside a CASE statement)

    Hello

    Your intuition is right...

    In Oracle NULL is a bit special:

    NULL = NULL is false

    NULL <> NULL is false

    Thus, instead of the parameter a NULL value in the CASE WHERE there is just set a value on the other side:

    NVL (vc_statut, 'Available') =: P32_STATUS

    That's all! Simple, clean and functional

  • Exclude duplicates on SQL where clause statement

    Hello!

    Are a few posibilities to exclude duplicates do not using aggregate sql in a main select functions?

    Priview SQL statement

    SELECT * FROM 
    (
    select id,hin_id,name,code,valid_date_from,valid_date_to
    from diaries
    )
     QRSLT 
     WHERE (hin_id = (SELECT NVL(historic_id,id)FROM tutions where id=/*???*/ 59615))
     AND NVL(valid_date_to,to_date('22.12.2999','dd.mm.yyyy')) <= (SELECT NVL(valid_date_to,to_date('22.12.2999','dd.mm.yyyy'))FROM tutions where id= /*???*/ 59615)
     AND trunc(valid_date_from) >=(SELECT trunc(valid_date_from)FROM tutions where id= /*???*/ 59615)
    ;
    

    The result

    ID HIN_ID NAME CODE VALID_DATE_TO VALID_DATE

    5051259564RE YOU0107.06.2013 16:32:1507.06.2013 16:33:28
    5051359564TT20207.06.2013 16:33:2307.06.2013 16:33:28
    5051559564TT20207.06.2013 16:33:2807.06.2013 16:34:42
    5051659564STEM0307.06.2013 16:34:3707.06.2013 16:34:42

    VALID_DATE_TO & VALID_DATE_FROM information AND

    07.06.2013 16:34:4215.07.2013 10:33:23

    In this case, I had doubles of the TT2 50513 entry in a statement identifier select main cant use agregate functions are still possible to exclude this result value by changing only the QLRST a WHERE clause (TRUNC must be here)

    THANKS FOR ANY ADVICE!

    FDA

    Try this,

    WITH QRSLT AS (SELECT id, hin_id, name, code, valid_date_from, valid_date_to FROM diaries)
    SELECT *
      FROM QRSLT A
     WHERE (A.hin_id = (SELECT NVL (historic_id, id)
                          FROM tutions
                         WHERE id =  /*???*/ 59615))
       AND NVL (A.valid_date_to, TO_DATE ('22.12.2999', 'dd.mm.yyyy')) <= (SELECT NVL (valid_date_to, TO_DATE ('22.12.2999', 'dd.mm.yyyy'))
                                                                             FROM tutions
                                                                            WHERE id =/*???*/ 59615)
       AND TRUNC (A.valid_date_from) >= (SELECT TRUNC (valid_date_from)
                                           FROM tutions
                                          WHERE id = /*???*/ 59615)
       AND A.valid_date_from = (SELECT MAX (B.valid_date_from)
                                  FROM QRSLT B
                                 WHERE A.Name = B.Name
                                   AND A.Code = B.Code
                                   AND A.hin_id = B.hin_id)
    

Maybe you are looking for

  • Missing after iTunes restore App news

    I just restored my Air iPad via iTunes. I couldn't remember the password encryption so I used my iPhone backup to restore my iPad because I could remember this password. Most things seem to be back to normal apart from the news application that is mi

  • the iPhone camera is muted iBook / audio sound

    I listen to an audio book. I stop to take a picture, but as soon as I activate the camera application, the audiobook is paused. I'm not sure that this was the case on older versions of iOS - but is there a way to disable in iOS9?. 3.1? I just want to

  • Portege M500 - cannot read overburned DVD + r

    Hi all running a Toshiba Portege M500, WXP SP2, the v3.30 BIOS HomeMatsu * a DVD-RAM UJ-842 drive with firmware 1.01. My problem is that the drive will not read overburned DVD + r - reports "disk may be corrupted or using a format that is not compati

  • Satellite P300D-12D descends

    Hello My drops of laptop down like 2 times a day. It happens when the computer is busy, as if I'm playing games. I think it's because my laptop gets hot, but how can I see if it is true? I am able to see the cause of this problem somewhere? Everest s

  • Need cable to connect my camera to the TV

    Hello I want to connect my laptop to the tv.the cables which should I buy? Help me please TKS. Davide