WHERE conditional clause with CASE statement

Hi experts,

I have a table:

CREATE THE TABLE HR. TABLE_Y

(

NUMBER (2),

ID NUMBER (2),

NUMBER (5) CALC

)

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(1, 1, 10);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(1, 2, 15);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(1, 3, 17);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(1, 4, 20);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(2, 2, 11);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(1, 2, 14);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(2, 3, 18);

Insert into human resources. TABLE_Y

(TYPE, ID, CALC)

Values

(3, 1, 30);

COMMIT;

SQL > SELECT * FROM TABLE_Y ORDER BY 1.2;

TYPE ID CALC

---------- ---------- ----------

1          1         10

1          2         15

1          3         17

1          4         20

2          1         14

2          2         11

2          3         18

3          1         30

I have the query as below,

I pass a TYPE and an IDENTITY value as a condition for a query.

SELECT THE TYPE, ID, CALC

OF TABLE_Y

WHERE TYPE =: TYPE

AND IN THE CASE OF IDENTITY

WHEN: TYPE = 1

THEN 2

WHEN: TYPE = 2

THEN 1

End

;

When passing a TYPE = 1

Output

ID of TYPE CALC

----------     ----------   ----------

1                2           15

When passing a TYPE = 2

Output

ID of TYPE CALC

----------    ----------      ----------

2               1               14

I want to move a TYPE and ID more values as a condition for a query.

I'm trying

SELECT THE VALUE TYPE, ID,

OF TABLE_Y

WHERE TYPE =: TYPE

AND IN THE CASE OF IDENTITY

WHEN: TYPE = 1

THEN (1,3)

WHEN: TYPE = 2

THEN (1,2)

End

;

But gives me error:

ORA-00907: lack of right parenthesis

During the passage of a desired output TYPE, = 1

TYPE ID CALC

----------  ----------    ----------

1            1            10

1            3            17

During the passage of a desired output TYPE, = 2

TYPE ID CALC

---------- ----------      ----------

2           1              14

2           2              11

Thanks in advance

BANNER

----------------------------------------------------------------

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - production

PL/SQL Release 10.2.0.4.0 - Production

CORE 10.2.0.4.0;     Production

AMT for Solaris: release 10.2.0.4.0 - Production

NLSRTL Version 10.2.0.4.0 - Production

Hello

Here's one way:

SELECT the type, id, value

OF table_Y

WHERE type =: type

AND ((: type = 1))

AND id IN (1, 3)

)

OR (: type = 2)

AND id IN (1, 2)

)

)

;

Tags: Database

Similar Questions

  • Based where conditional clause...

    dear team,
    i have following code..
    
    Declare
      gv_flag1 Varchar2(1)   := 'N';
      gv_flag2 Varchar2(1)   := 'N';
      gv_flag3 Varchar2(1)   := 'N';
      all_where1       Varchar2(250);
      all_where2       Varchar2(250);
      ALL_where3       Varchar2(250);
      V_QTY           Number;
    Begin
      If gv_flag1 = 'N' Then
         all_where1 := 'AND '||'V.OWNER = ''PROD''';
      End If;
      DBMS_OUTPUT.PUT_LINE(all_where1);
      
      If gv_flag2 = 'N' Then 
         all_where2 := 'AND '||'V.OPERATION NOT LIKE (''10%'')';
      End If;   
      DBMS_OUTPUT.PUT_LINE(all_where2);
      
      If gv_flag3 = 'N' Then
         all_where3 := 'AND '||'V.OPERATION NOT LIKE (''07%'')';
      End If;
      DBMS_OUTPUT.PUT_LINE(all_where3); 
       
      --select based on conditions..
    End; 
    
    NOW I want where conditions in select statment to be conditional...
    
    which means if flag1='N' and flag2 = 'N' then use both all_where1 and all_where2 in *where* clause statement...
    if flag1='N' and flag3 = 'N' then then use all_where1 and all_where 3 in *where* clause of select statement...
    if all there flag = 'N' then use all_where conditions in *where* clause of select statement...
    
    i have 3 flags, which means total of 9 combinations,  is there any simpler way to do such kind of thing??
    
    please assist me
    
    nic

    Nicloei W wrote:
    Hi Jeenesh,

    What happens if Flag2 = 'Y' and Indicateur3 = 'Y' in this case, I want only the condition with Flag1

    concerning
    NIC

    SQL> ed
    Wrote file afiedt.buf
    
      1  Declare
      2    gv_flag1 Varchar2(1)   := 'N';
      3    gv_flag2 Varchar2(1)   := 'Y';
      4    gv_flag3 Varchar2(1)   := 'Y';
      5    --all_where1       Varchar2(250);
      6    --all_where2       Varchar2(250);
      7    --ALL_where3       Varchar2(250);
      8    lc_query varchar2(1000):= 'select count(*) from test v ';
      9    lc_where varchar2(500) := ' where 1 = 1 ';
     10    V_QTY           Number;
     11  Begin
     12    If gv_flag1 = 'N' Then
     13       lc_where := lc_where||' AND V.OWNER = ''TEST''';
     14    End If;
     15    If gv_flag2 = 'N' Then
     16       lc_where := lc_where||' AND V.OPERATION NOT LIKE ''10%''';
     17    End If;
     18    If gv_flag3 = 'N' Then
     19       lc_where := lc_where||' AND V.OPERATION NOT LIKE ''07%''';
     20    End If;
     21    lc_query := lc_query||lc_where;
     22    dbms_output.put_line(lc_query);
     23    dbms_output.put_line('-----------');
     24    execute immediate lc_query into v_qty;
     25    dbms_output.put_line('Count: '||v_qty);
     26* End;
    SQL> /
    select count(*) from test v  where 1 = 1  AND V.OWNER = 'TEST'
    -----------
    Count: 3
    
    PL/SQL procedure successfully completed.
    
  • Select with case statement and a formula of the IIR

    Hi, I was looking to get help regarding a statement writing box with a statement select statement all. I tried to reproduce a formula IIf access well that just wanted to check that the query I wrote is correct, any advice would be appreciated.

    [code]

    Select *.

    Of

    (

    Select name, month, duration, volume, time_spent, date1, date2,.

    -case when 'date' > = 'date1' then '1' other '0' end as departure,.

    -case when 'date' < = "date2" then '1' other '0' as end ending

    Of

    (

    Select *.

    of call_1 cd

    inner join call_2 ON cd.name = cl.queue cl

    )

    )

    ;

    [/ code]

    I want to know is where I have my ' select name, etc, I would change that to select * to make it easier instead of typing all the field_names outside, but I don't know how to do and also what follows is 2 IIF formulas from an access database for the start of the final case statements so I just wanted check I wrote it correctly.

    [code]

    departure: IIf ([date] > = [date1], 1, 0)

    [/ code]

    [code]

    ending: IIf ([date] < = [date2], 1, 0)

    [/ code]

    Any advice would be appreciated.

    Hello

    Whenever you have any questions, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    Explain, using specific examples, how you get these results from these data.

    If the output depends on what anyone outside the application itself (for example, when it is run) and then include a few different examples and the results you want of each given the same sample data. For example, "if I run on November 19, 2013, while the results should be... because... but if I run between November 21 and November 27, then the results should be... because...". »
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

    318f20b8-a3d0-4FB4-bb0f-73785250b7d4 wrote:

    Hi, I was looking to get help regarding a statement writing box with a statement select statement all. I tried to reproduce a formula IIf access well that just wanted to check that the query I wrote is correct, any advice would be appreciated.

    [code]

    Select *.

    Of

    (

    Select name, month, duration, volume, time_spent, date1, date2,.

    -case when 'date' > = 'date1' then '1' other '0' end as departure,.

    -case when "date."<= 'date2'="" then="" '1'="" else="" '0'="" end="" as="">

    Of

    (

    Select *.

    of call_1 cd

    inner join call_2 ON cd.name = cl.queue cl

    )

    )

    ;

    [/ code]

    I want to know is where I have my ' select name, etc, I would change that to select * to make it easier instead of typing all the field_names outside, but I don't know how to do and also what follows is 2 IIF formulas from an access database for the start of the final case statements so I just wanted check I wrote it correctly.

    [code]

    departure: IIf([date]>=[date1],1,0)

    [/ code]

    [code]

    ending: IIf ([date]<>

    [/ code]

    Any advice would be appreciated.

    DATE is not a function of Oracle; in fact, it's a terrible name for a column or function, because it blends with the DATE data type.

    In Oracle, the function SYSDATE returns the date and time, according to the clock on the database server.  So, SYSDATE can return a value as November 19, 2013 06:33:15.   If you want to midnight the same day (i.e., November 19, 2013 00:00:00) and then use TRUNC (SYSDATE).

    String literals (for example the string which consists 5 characters d, a, t, e, and 1) go inside the single quotes. Numbers and the names of columns only.

    If you want to include all the columns, more some calculated values, in a SELECT clause, then you must use a name table or alias before the *.  (See select_2 below.  To do this, assign the alias j to display online.)

    Maybe you wanted to say something like:

    Select *-it's select_1

    de)

    Select j. *-it's select_2

    , case when SYSDATE > = date1 then 1 or 0 end as departure

    , case when SYSDATE<= date2="" then="" 1="" else="" 0="" end="" as="">

    de)

    Select *-it's select_3

    of call_1 cd

    inner join call_2 ON cd.name = cl.queue cl

    ) j

    )

    ;

    As mentioned in a previous answer, you should be careful about the use of "SELECT *" in production code.

    In select_3, it would be better if you explicitly listed the columns you need.  It can improve efficiency and maintenance.  In this request, queue and the name will be always the same, so you probably don't want to include both of them in the result set, in any case.

    In select_1 and select_2, it is acceptable to use "SELECT *", assuming that select_3 is fixed.

    Moreover, there is no point in using subqueries here.  You can get the same results simply in a single query, without any subqueries.

  • Condition within a where condition clause

    Dear friends,
    I am a beginner in PL/SQL and I need your help.
    I'll show you my code (it does not):
    declare
        var_mese VARCHAR2 (2);
    begin
        select max(to_number(MESE)) 
        into var_mese
        from NOC_MONITORAGGIO;
           SELECT count(*)
            FROM noc_sdoc_work a,
                 noc_sdoc b,
                 NOC_MONITORAGGIO
           WHERE NVL(a.CD_STRUTTURA,'x') = NVL(b.CD_STRUTTURA,'x')
             AND NVL(a.CD_SUB_STRUTTURA,'x') = NVL(b.CD_SUB_STRUTTURA,'x')
             AND NVL(a.NR_PRATICA,'x') = NVL(b.NR_PRATICA,'x')
             AND NVL(a.ASL,'x') = NVL(b.ASL,'x')
             AND b.STATO IN ( 1, 2 )         
            and
                if  var_mese in ('03','04','05','06','07','08','09','10','11','12') then
                    NOC_MONITORAGGIO.ANNO_GESTIONE = b.ANNO;
                else
                    NOC_MONITORAGGIO.ANNO_GESTIONE = to_number(b.ANNO)-1;
                end if;
    end;
    My problem is here:
            and
                if  var_mese in ('03','04','05','06','07','08','09','10','11','12') then
                    NOC_MONITORAGGIO.ANNO_GESTIONE = b.ANNO;
                else
                    NOC_MONITORAGGIO.ANNO_GESTIONE = to_number(b.ANNO)-1;
                end if;
    I need a nested where condition

    Thank you
    Leo

    You can code box that using a statement in the WHERE clause:

     and NOC_MONITORAGGIO.ANNO_GESTIONE =
                case
                   when var_mese in ('03','04','05','06','07','08','09','10','11','12') then b.ANNO
                   else to_number(b.ANNO)-1
                end
    
  • WHERE condition in a sql statement

    Hello people,

    I have a screen with data grid where one of the columns is STATES_CODES and other VOLUMES one. Grid consists of several lines. I need to create a CUSTOM SEARCH field that will be filtering on this database.

    Unfortunately a customer wants to enter a search string with custom functions: '+' (plus) to include in the selection of the result and "-" (less) to exclude from the selection of the results. For example: "+ IT + -100". Based on this string, I need to return only the States HE and and exclude volumes with a value of 100.

    A tip how to build a query where condition, so it will be more effective as possible?

    Simple example:

    test data:

    with t (select 'IT' State, volume 90 of any union double

    Select 'IT' State, 100 volumes of all the double union

    Select 'IT' State, 100 volumes of all the double union

    Select 'OF' State, 90 volume of all the double union

    Select 'OF' State, in 100 volumes of all the double union

    Select 'OF' State, in 100 volumes of all the double union

    Select 'NL' State, 90 volume of all the double union

    Select 'NL' State, 100 volumes of all the double union

    Select 'NL' State, 100 volumes of all the double union

    Select 'FR' State, 90 volume of all the double union

    Select 'FR' State, in 100 volumes of all the double union

    Select 'FR' State, in 100 volumes of all the double union

    Select 'ARE' State, 90 volume of all the double union

    Select 'ARE' State, in 100 volumes of all the double union

    Select 'ARE' State, 100 volumes of double

    )

    Select * from t

    Search string: "+ IT + - 100".

    I thought to somehow using regular expressions REGEXP_LIKE condition, but do not know how to change the entered search string in above format as long as the regular expression.

    And if you want to be specific about the characters representing the State and the numbers represent the amount then you will need to divide them into more...

    SQL > with t (select 'IT' State, 90 volume of all the double union)
    2 Select 'IT' State, 100 volumes of all the double union
    3 select 'IT' State, 100 volumes of all the double union
    4. Select 'FROM' State, 90 volume of all the double union


    5. Select 'FROM' State, in 100 volumes of all the double union
    6. Select 'FROM' State, in 100 volumes of all the double union
    7 select 'NL' State, 90 volume of all the double union
    8 select 'NL' State, 100 volumes of all the double union
    9 select 'NL' State, 100 volumes of all the double union
    10. Select 'FR' State, 90 volume of all the double union
    11. Select 'FR' State, in 100 volumes of all the double union
    12. Select 'FR' State, in 100 volumes of all the double union
    13. Select 'ARE' State, 90 volume of all the double union
    14 select 'ARE' State, in 100 volumes of all the double union
    15 select 'ARE' State, in 100 volumes of double
    16              )
    17, ch. as (select "+ IT + -100"as the double criterion)
    18, split as (select regexp_substr (trim (', ' regexp_replace (criteria,'([+ -]) ', ', \1')), "[^,] +', 1, level") as a criterion of)
    19 c
    20 connect the regexp_substr (trim (', ' regexp_replace (criteria,'([+ -]) ', ', \1')), "[^,] +', 1, level") is not null
    21                 )
    22, inc_state as (select substr(criteria,2) as a criterion
    23 split
    where the 24 regexp_like(criteria,'^\+[A-Z]+')
    25                     )
    26, inc_vol as (select substr(criteria,2) as a criterion
    27 split
    where the 28 regexp_like(criteria,'^\+[0-9]+')
    29                    )
    30, exc_state as (select substr(criteria,2) as a criterion
    31 split
    where the 32 regexp_like(criteria,'^\-[A-Z]+')
    33                     )
    34, exc_vol as (select substr(criteria,2) as a criterion
    35 split
    where the 36 regexp_like(criteria,'^\-[0-9]+')
    37                    )
    38-
    39 select *.
    40 t
    where the 41 (exists (select 1
    inc_state 42
    43 where inc_state.criteria = t.state
    ((44) or (select count (*) in inc_state) = 0)
    45 and (exists (select 1
    46 by inc_vol
    where the 47 inc_vol.criteria = t.volume
    ((48) or (select count (*) in inc_vol) = 0)
    49 and (not exists (select 1
    50 to exc_state
    where the 51 exc_state.criteria = t.state
    ((52) or (select count (*) in exc_state) = 0)
    53 and (not exists (select 1
    exc_vol 54
    where the 55 exc_vol.criteria = t.volume
    ((56) or (select count (*) in exc_vol) = 0)
    57.

    ST VOLUME
    -- ----------
    HE 90
    90

  • Problem with CASE statements

    Hi all

    I have a calculation in a report of Discoverer Desktop with 29 instructions BOX and still 2 case statements more to add. The problem is that my report freezes and fails to run due to the excessive number of case statements. I need to put the 31 statemnts cases in the report, how do I do this? Is there another way to do it without causing the report to freeze?

    Below is part of my instructions box just to show you what I'm doing:

    CASE WHEN Ex = 'MM' AND THEN of OTHER NULL result (', ')
    CASES WHERE Ex = "PP" AND result IN (40,35,30,25) THEN NULL OTHERWISE
    CASE WHEN Ex = he's ' AND result IN (' a ',' B', 'C', d ') THEN NULL OTHERWISE
    -CASE WHEN Ex = 'IO' AND THEN of OTHER NULL result ('Distinction', 'Merit')
    -CASE WHEN Ex IN ('ZZ', 'WW', 'SS', 'KK') AND ('PASS', 'P') THEN to ANOTHER NULL result...

    The last statement should be BOX WHEN... THEN NULL OTHERWISE 1 END END END END...

    Help, please. Thank you.

    Hello
    You have reached the limit of characters in a calculation.

    Try to set into a single statement as follows:

    CASE WHEN Ex = 'MM' AND THEN of OTHER NULL result (', ')
    WHEN Ex = "PP" AND as a result (40,35,30,25) THEN NULL OTHERWISE
    WHEN Ex = he's ' AND result IN (' a ',' B', 'C', d ') THEN NULL OTHERWISE
    WHEN Ex = 'IO' AND THEN of OTHER NULL result ('Distinction', 'Merit')
    WHEN Ex IN ('ZZ', 'WW', 'SS', 'KK') AND ('PASS', 'P') THEN to ANOTHER NULL result...

    The last statement should be BOX WHEN... THEN NULL OTHERWISE 1 END

    Another option is to combine all the statements of NULL in a series of gold options like this:

    BOX WHEN
    (Ex = 'MM' AND the result in (', am')) OR
    (Ex = "PP" AND result IN (40,35,30,25)) OR
    (Ex = he's ' AND translated BY (' a ',' B', 'C', ')) OR
    (Ex = 'IO' AND as a result ('Distinction', "according to merit")) OR
    (Ex IN ("ZZ", "WW", "SS", "KK") AND result IN ('PASS', 'P')) THEN ANOTHER NULL...

    Best wishes
    Michael

  • Problem with Case statement

    I try to use a case statement in a report of the Apex, but I get an error message.

    It comes to my sql

    CASE WHEN EXISTS (select stg.extkey, distsubtrpgrpid

    of udm_da da

    udm_di di

    udm_distsubtrpgrp stg

    where di.disttransgrpid = stg.disttransgrpid

    and da.daid = di.daid

    and da.suid = su.suid)

    THEN

    Select stg.extkey

    of udm_da da.

    udm_di di,

    udm_distsubtrpgrp stg

    where di.disttransgrpid = stg.disttransgrpid

    and da.daid = di.daid

    and da.suid = ' | Qul SUID. '

    Stg.extkey group, distsubtrpgrpid

    Union

    Select stg.extkey

    of stg udm_distsubtrpgrp

    where distsubtrpgrpid! = - 1

    and distsubtrpgrpid = ' | NVL (su.distsubtrpgrpid-1). '

    order by 1

    WHEN su.distsubtrpgrpid IS NOT NULL

    THEN

    Select stg.extkey, distsubtrpgrpid

    of stg udm_distsubtrpgrp

    where distsubtrpgrpid! = - 1

    and distsubtrpgrpid = ' | NVL (su.distsubtrpgrpid-1). '

    order by 1

    ON THE OTHER

    NULL VALUE

    END DISTSUBTRPGRP_1

    Any help appreciated

    Gus

    GusC wrote:

    Yes you are right.

    My sql must be

    CASE WHEN su.distsubtrpgrpid IS NOT NULL

    THEN

    Select stg.extkey, distsubtrpgrpid

    of stg udm_distsubtrpgrp

    where distsubtrpgrpid! = - 1

    and distsubtrpgrpid = su.distsubtrpgrpid

    ON THE OTHER

    NULL VALUE

    END DISTSUBTRPGRP_1

    But the report won't save

    Gus

    No, your SQL is not that.

    The select statement that you put online here, aside from being not not in brackets, is the selection of two values, but you can provide two values for the result column.

    Without knowing who the SQL statement, we can highlight only the obvious questions.

  • Shot summary of a where condition clause...

    I have a form that displays the code material and this number in which different warehouses...

    It is possible to create an element of the summary screen is based on the place where the condition

    for ex.
    SQL> select  sum(arar) from tbs;
    
     SUM(ARAR)
    ----------
          1488
    
    for this query i can create a  display item and in the propery  pallete  i can select mode as summary,
    summary function as count, and select respective block and item...
    
    what i want to know is can this be based on where condition 
    some thing like the count shoudn't include where the column values is zero
    SQL> select count(arar) from tbs where arar!='0';
    
    COUNT(ARAR)
    -----------
            144
    Published by: Chase Suhail on November 9, 2010 22:45

    Hello

    Create a column of formulas no database in the same block to say 'NON_ZERO.
    The formula for the column would be: SET_NON_ZERO - this function returns 1 if arar is non-zero and 0 if it is zero.

    FUNCTION SET_NON_ZERO RETURN NUMBER IS
    BEGIN
     IF :ARAR = 0 THEN
       RETURN (0);
     ELSE
       RETURN (1);
     END IF;
    END;
    

    Now you can create a column of synthesis and use the text-to-speech function 'sum' and item summarised as "NON_ZERO.

    I hope this helps.

    Best regards

    Arif Khadas

  • ORA-01427 with WHERE clause and CASE statement

    Hi guys,.
    I get the error ORA-01427 when you try to select * from my dimension table
    When the parameter: P_HZA is 4. I used the keyword IN but the error still appears.
    no idea why?

    Select * from testtab
    where mycar = 4
    and "coretable". "" HZA ' IN
    CASE
    When: P_HZA = "4" then (button SELECTION OF DIM_HZA)
    other: P_HZA
    END

    Thank you

    you missed table_name and also this can cause several error so lines (ORA-01427: einreihig subquery returns more than one line)

    SELECT *
      FROM tableA
     WHERE mycar = 4
           AND "coretable"."HZA" IN
                  CASE
                     WHEN :P_HZA = '4' THEN (SELECT key FROM DIM_HZA)   ---- may be you need to have max  to get only one value
                     ELSE :P_HZA
                  END
    

    See you soon,.
    Manik.

  • Group By with Case statement

    Hello

    I have a question I want to return a different group of list depending on a variable of common past in? It sounds easy, but I can't understand it >

    Example 1 if the: P380_COURIER_TYPE = 'All' I want the group by being 'group by tour' - a single column

    Example 2 If the: P380_COURIER_TYPE <>'All' I wish that the group by to "group by tour, exp_del_channel '--2 columns

    Select

    Tour as a 'Tour '.

    Exp_del_channel as 'Courier Type' - it can be removed from the select statement because it is not necessary in the report.

    , sum (pan_only) "Pan" only

    Of

    VW_VOLUME_PREADVICE_REPORT APV

    where (: P380_COURIER_TYPE = 'All' or exp_del_channel =: P380_COURIER_TYPE)

    Touring band, exp_del_channel

    not tested

    select
    Tour as "Tour"
    ,nvl(case when :P380_COURIER_TYPE<>'ALL' then exp_del_channel end,'ALL') as "Courier Type"   -- this can be removed from the select statement as it is not required in the report.
    ,sum(pan_only) as "Pan Only"
    from
    VW_VOLUME_PREADVICE_REPORT vpa
    where (:P380_COURIER_TYPE ='All' or exp_del_channel =:P380_COURIER_TYPE)
    group by tour, case when :P380_COURIER_TYPE<>'ALL' then exp_del_channel end
    
  • Where conditional Clause based on the length of the field

    Hello people,
    I tried to search for this scenario in OTN and not been able to find any success so I will post the question here.

    I have two tables - with the detail records and the other being a table of codes. I need to join these two tables based on the code and the length of the code. Let me explain using examples.

    Scripts for creating the table and inserts
    create table ILLNESS_CODES(illness_code varchar2(4), illness_description varchar2(100));
    create table PATIENT_TB(patient_id varchar2(4), primary_cause varchar2(4));
    
    insert into illness_codes values('B10', 'Flu');
    insert into illness_codes values('B30', 'Hepatitis');
    insert into illness_codes values('B301', 'Hepatitis A');
    insert into illness_codes values('B302', 'Hepatitis B');
    insert into illness_codes values('B303', 'Hepatitis C');
    
    insert into patient_tb values ('1001', 'B101');
    insert into patient_tb values ('1002', 'B102');
    insert into patient_tb values ('1003', 'B30');
    insert into patient_tb values ('1004', 'B301');
    insert into patient_tb values ('1005', 'B302');
    insert into patient_tb values ('1006', 'B302');
    insert into patient_tb values ('1007', 'B303');
    insert into patient_tb values ('1008', 'B30');
    As you can see that patients * 1001 * and * 1002 * have no codes in the Master table. In this case, I want only the first 3 characters of the ILLNESS_CODES table.
    However, for B30, it has a Code and the same for B301, B302 B303 where I would meet the description based on the exact code.

    Example of output
    Patient ID   Illness Description
    =====================================
    1001         Flu
    1002         Flu
    1003         Hepatitis
    1004         Hepatitis A
    1005         Hepatitis B
    1006         Hepatitis B
    1007         Hepatitis C
    1008         Hepatitis
    Thanks in advance!
    SELECT p.*,
           NVL ( (SELECT illness_description
                    FROM ILLNESS_CODES I
                   WHERE i.illness_code = p.primary_cause),
                (SELECT illness_description
                   FROM ILLNESS_CODES I
                  WHERE i.illness_code = SUBSTR (p.primary_cause, 1, 3)))
      FROM PATIENT_TB p;
    
  • "Invalid column" on the place where several clauses with subqueries and cfqueryparam

    I see a behavior in the cfquery coldfusion I would like to find an exmplanation for.  I have a query that performs a subquery in the select part and if I have several where lines, I get a message 'invalid column name' to find out where my second article, but only when I'm with cfqueryparam

    For example, on what follows, I get "invalid column name «position_id»»

    SELECT department_staff_tbl.*,
    (SELECT max (bookmark_id)
    OF bookmarked_items_tbl
    WHERE item_id = department_staff_tbl.staff_id

    ) AS bookmark_id
    OF department_staff_tbl
    WHERE department_id = < cfqueryparam value = "" #arguments.deptid # "cfsqltype ="cf_sql_integer">"
    AND position_id = < cfqueryparam value = "" #arguments.posid # "cfsqltype ="cf_sql_integer">"
    AND staff_id = < cfqueryparam value = "" #arguments.staffid # "cfsqltype ="cf_sql_integer">"

    If I change the order of my where clause staff_id is first, and then he said "department_id" is an invalid column.

    If I don't have a where clause, it works.  (i.e. WHERE position_id = < cfqueryparam value = "" #arguments.posid # "cfsqltype =" cf_sql_integer">).

    If I remove the where clause of my subquery (WHERE item_id = department_staff_tbl.staff_id) it works.

    This also works if I remove the cfqueryparam where my article so that my query looks like this:

    SELECT department_staff_tbl.*,
    (SELECT max (bookmark_id)
    OF bookmarked_items_tbl
    WHERE item_id = department_staff_tbl.staff_id

    ) AS bookmark_id
    OF department_staff_tbl
    WHERE department_id = #arguments.deptid #.
    AND position_id = #arguments.posid #.
    AND staff_id = #arguments.staffid #.

    Any thoughts?

    A more conventional way to enter your data is without the subquery.  Something like that

    Select fields, the bookmarkid max (bookmark_id)

    from bookmarked_items__tbl join department_staff_tbl on staff_id = bookmark_id

    When etc.

    Some field group

  • WHERE condition has decoded the statement

    Hi all

    I have a requirement to have two or three conditions based on a condition in the WHERE clause. Please do the needful. Thank you.

    If the context value is PROJECTS BILLS, then must have conditons

    (due_date-nvl(rt.printing_lead_days,31)) < sysdate
    and aps.customer_trx_id = rct.customer_trx_id
    and rt.term_id (+) = rct.term_id
    and aps.customer_trx_id = (select customer_trx_id
    of ra_customer_trx_all
    where trx_number =
    (: $FLEX$ .AR_RAXINV_TRX_NUMBER)

    If the context value is other than the PROJECT INVOICES, the

    (aps.customer_trx_id = rct.customer_trx_id
    and rt.term_id (+) = rct.term_id
    and aps.customer_trx_id = (select customer_trx_id
    of ra_customer_trx_all
    where trx_number =
    (: $FLEX$ .AR_RAXINV_TRX_NUMBER)


    Ex:

    I tried, but received the error.

    Select terms_sequence_number
    ar_payment_schedules_all aps, RA_CUSTOMER_TRX_ALL CTN, ra_terms rt
    where
    DECODE (rct.interface_header_context,'PROJECTS BILLS, (aps.customer_trx_id = rct.customer_trx_id)
    and rt.term_id (+) = rct.term_id
    and aps.customer_trx_id = (select customer_trx_id
    of ra_customer_trx_all
    where trx_number = '80000062')),
    ((due_date-nvl(rt.printing_lead_days,31)) < sysdate and)
    APS.customer_trx_id = rct.customer_trx_id
    and rt.term_id (+) = rct.term_id
    and aps.customer_trx_id = (select customer_trx_id
    of ra_customer_trx_all
    where trx_number = '80000062')))

    Thank you
    Abdul

    Select...
    Of...
    where (rct.interface_header_context = 'INVOICES of PROJECTS'
    OR (rct.interface_header_context! = "PROJECTS BILLS" and (due_date-nvl(rt.printing_lead_days,31))
    )< sysdate="">
    and aps.customer_trx_id = rct.customer_trx_id
    and rt.term_id = rct.term_id
    and aps.customer_trx_id = (select customer_trx_id from the ra_customer_trx_all where trx_number = '80000062')

  • How to use the Substring function with Case statement.

    Hi all

    I have a requirement where I have to use the substring function on the ground for the report criteria.
    FOR EXAMPLE
    I branch domain name where I have all the information of the branch names, now some of the branch names are too large with an extension after the name.
    now I want substing it, but the length of characters varies for each branch.
    so is it possible where we use a box where we can define if the character of name plug exceed a value then he substing with this length.

    Try something like this:

    LENGTH WHEN CARTER (tablename. Branch_Name) > n THEN SUBSTRING (...) Of ANOTHER tablename. Branch_Name END

    where n is the number of characters that you want to start the break.

  • How to use where condition in an update statement

    Oracle forms 6i
    Hai All

    I generate table of attendance. My name of the table is daily_attend and fields

    name varchar

    empcode number

    Date of the respondent

    Outtime date

    number of working_hrs

    Date of Attend_date

    Attend_status varchar

    So here, I calculate the hours of work of an employee by

    Update dail_att set wtime = lpad (((outtime-intime) * 24 * 60), 4, 0);

    It works great for example for one day

    When I want to do on a daily basis, I have to give in the where Clause and how I can give attend_date in where Clause

    Thank you in advance

    Srikkanth.M

    change this to more well-intentioned and reusable procedure... Remove commit inside of the procedure

    PROCEDURE W_TIME(p_date date)
    
    IS
    BEGIN
    update dail_att set wtime= lpad(((outtime-intime)*24*60),4,0)
    where trunc(attend_date) = trunc(p_date);
    
    END;
    

    and whenever you want to call it simply call

    begin
     ......
     w_time('12-APR-2010');
     commit;
    ....
    

Maybe you are looking for

  • My pavilion dv2418nr wireless light stays orange

    My pavilion dv2418nr wireless light stays orange, you don't go to the Blue will not recognize the connections surrounding neighborhood.  ISP says that the router is fine, this is the switch of the laptop not allowing the card read wireless connection

  • Upgrade to iOS 9.2, now music landing page picture of daughter apple. What is the solution?

    I just updated my new iOS 9.2 iphone6s the music worked fine until this update. Now apple musicis landing on a girl photo pic... not to go forward. Impossible to play songs. What is the solution?

  • DHT 11 sensor and NI USB-6009

    Hi all I am a new user of LabView with only a CLAD certification so please forgive my ignorance on the subject at hand.  I try to use a DHT 11 temperature sensor and moisture (Backgrounder: http://www.micro4you.com/files/sensor/DHT11.pdf ) with a cas

  • XP admin account is hidden and in safe mode F8 annoys me but I cannot view the admin account.

    The admin account is hidden and it seems as my user account, but what built allowed me to install the drivers it isn't now.  Impossible to install a wireless card to get on the web.I followed the previous advice and used the SafeMode (F8) and got to

  • Why can I not sign out of hotmail?

    I can connect to hotmail, but everytime I try to log I get an error message like for example "Internet Explorer cannot display the webpage." And when I look at the URL in the search box, it displays the message "shared the disconnection.  What does t