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;

Tags: Database

Similar 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

  • 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.
    
  • dynamic logic in SQL WHERE clause based on the value of the ELEMENT

    Hello

    I have a report based on the following SQL query.
       select dept_no,
           dept_name,
           dept_loc
      from dept
     where dept_loc = :P1_DEPT_LOC
       // If P1_DEPT_LOC is not null, I want the WHERE clause and IF  P1_DEPT_LOC is null, then I don't need the WHERE clause.
       // how can I build this logic in same SQL
    Thank you
    Deepak

    Published by: Deepak_J on March 11, 2010 16:37

    where: P1_DEPT_LOC IS NULL or dept_loc =: P1_DEPT_LOC

    If dept_loc is not null better would be

    where dept_loc = coalesce(:P1_DEPT_LOC,dept_loc)

  • conditional merge based on the date

    Need help on a merge statement. I need to merge two dataset. But when the key match, I also need to cpmpared the date before I merege it.
    See code below
    drop table x1;
    
    drop table y1;
    
    select * from x1;
    
    select * from y1;
    
    create table x1 (id number,name varchar2(10),cdate date,status varchar2(1));
    
    create table y1 (id number,name varchar2(10),cdate date,status varchar2(1));
    
    insert into x1 select 1,'A',sysdate - 100,'Y' from dual;
    
    insert into y1 select 1,'A',sysdate - 50,'N'  from dual ;--should change x1.status to y1.status  and x1.cdate to y1.cdate as y1.cdate > x1.cdate
    
    insert into x1 select 2,'B',sysdate - 20,'Y' from dual;
    
    insert into y1 select 2,'B',sysdate - 120,'N' from dual ;--should leave status in x1 alone
    
    insert into y1 select 3,'c',sysdate - 10,'Y' from dual; --should do an insert on x1 
    
    commit;
    
    --how do I do a conditional merge (key=id,name)
    -- based on the cdate. If y1.cdate >= x1.cdate then take the y1.status  and y1.cdate
    
    --desire output
    select * from x1
    1     A     01/01/2012 10:54:35 AM     N
    2     B     1/31/2012 10:54:37 AM     Y
    3     C     1/31/2012 10:54:37 AM     Y

    Hello

    You can add a WHERE clause to the WHEN MATCHED THEN UPDATE section.

    WHERE   x1.cdate  < y1.cdate
    

    Thanks for posting the CREATE TABLE and INSERT statements; It is very useful. It would also help if you have validated your MERGE statement, even if it does not work perfectly.

  • 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
    
  • Accident stroke multicolor where color is based on the color of the pixel at the edge

    Hi and first of all sorry for the terrible title. I've been looking for a way to create something like a regular edge effect, but the color should be based on the color of the edge pixel. I need something like this because when I am doing textures for my models and put them in a game engine, I have a problem where the white lines are showing on a certain distance from my model. The solution to this problem is to add color around the islands of uv that is similar to the color of their edges. What I've done until now is put almost every island uv on a layer separate behind the main texture, so when I want to resize this layer copied it would do the trick. But this is a long process for the more than 50 models, so I thought that maybe someone has a better idea of how to approach this problem. Thank you

    Do not know if this fits your needs but you can try, after performing a selection of pixels of the layer, select > modify > border, choose a couple or a few pixels and then edit > fill > Content Aware Fill. You could also simply extend the selection with Select > modify > Expand and then do an intersection of the selection with the original selection, and then Content Aware Fill, but I don't know if the additional step will buy you anything. If that works, create an Action and it in batches on all files, could speed things up.

  • Textarea conditional display based on the value of the checkbox.

    Hi all

    I have a question regarding the conditional display of an article based on the value of the checkbox.

    I have a checkbox in a region, which is a STATIC LOV:; Y and the text box in the same region. Now I want to show this textarea only if the check box is selected. ??

    Please suggest how to achieve this.

    Thank you

    DC

    Hello

    I guess that you are 4.2 Apex as you haven't shared the same.

    • Create a dynamic Action on change of box which should present the value of the checkbox and update the TextArea element.
    • Then continue as conditional Textarea with checkbox value.

    BR,

    Patrick

  • Conditional display based on the value of line

    I have a report table with several lines on it.  I need to post a link to another page of user action if a value in the line corresponds to an attribute of the user's role, so they can act on the item.  Other display or update the document would not be able to run the link.

    The logic of nickname would go something like this:

    If user_role = role_this_line_item then

    display approval_link

    on the other

    Hide approval_link

    end if

    I know how to write pl/sql to return to the display of the correct state of true/false, but I can't find out how to reference the value on the display line.  I tried: role_this_line_item, role_this_line_item, #role_this_line_item # and none of them return one value other than false.  How to refer to an attribute on the current line of the decision to hide/show correct?

    Al

    Al Wondra says:

    I use V4.2.6 Apex in local installation of Oracle 11 g XE.  I tried with IE, Firefox and Chrome.  None has the conditional display.

    The report is a standard report created using the Apex report wizard.  I'm one of the columns (the ID of the line) changing to conditionally a link to a page to data entry.  I only need one line at a time to be the active link.  No line should have a link, if the user is not the role is required to perform this action.

    You have described my question exactly, it seems, that the conditional rendering is applied to the entire column.  I need that it is applied to specific lines.  Some lines (1) will be the active link.  All other lines does not display the link.  Is this possible?

    If you want the value of the ID to display for each row returned by the report, but only rendered as a link, if the condition is true? The values used in the State come "role_this_line_item" seems to be a value of a column in the report. "User_role" is also a column value?

    The simplest option is to modify the column attribute display as for the Standard report columnID column and conditionally generate the link in the report query:

    select
        ...
      , case
          when user_role = role_this_line_item
          then
            'where DATA_ENTRY is the 'data entry page' target number or alias of the page.

  • Basic conditional script based on the number of lines in a text layer?

    Just received help here to write a conditional script by using the font size of a text layer. Thank you all for this.

    The next hurdle is a conditional script using the number of lines in a text layer in the differential. Far as I can tell it not the layer details which specify the number of lines. Just '\r' for a return in the idTxt descriptor. Nothing seems to stand for a soft return as far as I can tell. I'll probably have to be able to count the lines with soft and hard returns.

    Any thoughts?

    I was playing around with this a bit and here is a small script to determine the number of rows. This is based on the self in the head, that I calculated to environ.108 times the size of the font.

    #target photoshop
    app.preferences.rulerUnits = Units.POINTS;
    var doc = activeDocument
    var tL = doc.activeLayer
    var h = tL.bounds[3]-tL.bounds[1]
    var per = ((h/5)-(tL.textItem.size))/tL.textItem.size
    var num = h/(tL.textItem.size *1.108)
    
    alert(Math.round( num))
    
  • order by clause based on the value of the parameter

    Hello

    I use reports 6i with db 10 g

    I need to create a report sorted in the order of four fields. There is a field selection parameter which should come first in the order by clause.

    IE the user has available to display the report in a sorted order selected

    Assume that the four fields are emp_id, name, dept_id desig.

    If the user selects dept_id, then the order by clause should be in order of dept_id emp_id, name, desig

    Please help solve this scenario.

    Thank you

    Hi Clement,.

    Use the query as follows:

    select column_name1, column_name1, ....
    from  table_name
    &order_by_clause
    

    Now in the trigger AFTER-PARAMETER-SHAPE has the order by clause below according to requirements:

    if :sort_column = dept_id then
    
      :order_by_clause := 'order by dept_id,emp_id,name,desig';
    
    else
    
      :order_by_clause := 'order by emp_id, dept_id, name,desig';
    
    end if;
     
    

    Here's sort_column setting user who will be selected by the user, provide the list of values for it.

    Hope this helps

    Best regards

    Arif Khadas

  • conditionally display name of the field page apex LABEL in the email of the Apex.

    Hello
    I'm using Apex email process.

    I have a page where I have check conditions. ONLY WHEN the checkbox is enabled in the 'yes', I need to display this field in emails.
    Please notify

    field 1-birthday card order YES NO
    field 2-marriage card order YES NO



    If 1 is checked YES and 2 - is checked that NO, I need to insert only

    The NAME of the field LABEL 1. like...

    * 1 - birthday card order *. How? Please notify.

    Thank you
    KP

    Published by: user8612301 on March 7, 2013 13:47

    You can query the integrated views apex

    select LABEL from apex_application_page_items
    where application_id =110
    and page_id = 18
    //OR
    where ITEM_NAME = 'PX_ITEM_NAME'
    
  • The default value based on the field from another table to a custom object

    I'm trying to set the default value to a field in the custom object to the value of a field of account. I tried the syntax 50 ways different and just don't get the case. The label for the account field displays the form of s/n, the integration of the tag is ltDBA_ACCT and it appears in the fx reports area as Account.Text_22.

    The field of custom object that I am triying update is also called s/n, which was originally the required field 'NAME '. The name of the table, account, should it be included? Do I need a function to the field?

    I've updated the external ID using the line with syntex < ID > ID (at least higher ID) so I know that it is possible to define a default value, but s / < n >, < ltDBA_ACCT >, 'account '. "" S/n "and so on are simply not working.

    If anyone knows how to get into what I would be really grateful for the help.

    OK, so if you default a field to the value of another object, you must use the JoinFieldValue function. I think you understand that, based on your original post, but I want to be sure you do.

    Then this won't work by default if the folder is created from the object that you want to join the because a default works in record creation and the ID must be available so that it works correctly. It will not work if you choose the record of the related object after that registration of the custom object is created. You can set the default after, but that does not meet your requirements.

    The syntax of the default are the following: JoinFieldValue (ref_record_type, foreign_key, field_name).

    In your case, ref_record_type is '', foreign_key is [] and field_name is ''. The best way is to determine what is the name of the field to create a new workflow for the account and use the Workflow Rule Condition expression builder to choose your field ("DBA") in the list. The value returned by the expression builder must be placed in the field_name variable in the function JoinFieldValue (minus the parentheses and quotes).

    Give it a shot and let me know how you do.
    Thom

  • Calculate the price based on the field

    Hello

    My apologies, I know it's a very basic question, but I can't understand the correct syntax for the custom calculation Script necessary to produce the total cost in my form (I just received Acrobat Pro today).

    I have the user fill in the quantity field and then I try to set the TOTAL field to calculate the cost by multiplying the quantity by the price of $29. I thought it was simple, like writing a formula in Excel, but it doesn't seem to work that way and I can not find all resources on Adobe.com or online who point me in the right direction. Any help anyone can offer would be greatly appreciated.

    It might be easier to use the option of simplified field notation, in which case you must enter:

    29 * QTY

    If you want to use a custom calculation script, it could be something like:

    Custom calculation for a field text script

    (function () {}

    Get the value of the quantity, as number

    var qty = + getField("QTY").value;

    Calculate the value of this field if the quantity is greater than 0

    If (Qty > 0) {}

    Event.Value = util.printf ("%.2f", 29 * qty);  round to the nearest hundred

    } else {}

    Event.Value = "";   This field blank

    }

    })();

    If this option gives you more flexibility. you to round off and empty the field. You can also perform additional checks to ensure that the amount of sense (for example, a positive integer within a certain range) you can find more information in the Acrobat JavaScript reference, and here is a link to a tutorial introduction on how to set up calculations in PDF forms: https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations

    Be sure to set the computed fields read-only so that the user does not try to interact with them.

  • Conditional WHERE clause based on the input of parameter

    When the restriction of data based on a parameter I generally use the following syntax:

    AND p.start_date = NVL (p_start_date, p.start_date)

    so IF the parameter is null, THEN I join only on itself, OTHERWISE we filter on parameter.

    New requirement means greater than or equal to, so I tried this:

    AND THE CASE
    WHERE (p_start_date = NULL) THEN
    p.start_date = p.start_date
    ON THE OTHER
    p.start_date > = p_start_date
    END;

    but, at least, is syntactically incorrect.

    Any suggestions?

    Thank you!

    Do not use = with NULL - change IS null

Maybe you are looking for

  • Re: error driver.atikmpag 7670 m

    I can't even with the same card 7670 m tried bunch of pilots noting working same question but I m on win 8 64 bit

  • Drivers Compaq Presario v3250au windows xp

    Hello I need Windows XP drivers for my Compaq Presario V3250AU laptop. The serial number is [thank you for visiting the Forums from HP Support. I'm sorry to inform you that your message needs some changes to remove personal information].. Thanks in a

  • IMAQdx attributes listed in the xml file not found in labview

    Hello Some attributes of my camera our watch camera (DALSA CamExpert) software but are not displayed in MAX. When I opened the xml file, I don't see a difference between the attributes shown and hidden. Why LabView displays all possible attributes? T

  • Why my custom control consuming 30% of the CPU power?

    Hello on my laptot C2D 2.0 GHZ if I open just VI with custom control I developed it consumes about 30% of the CPU power. Could someone explain why? Is also my problem with this control? I see it with 8.5 and 8.6 as well. It must be linked to its repr

  • Windows 7 installation problem Pavilion dv6-6c45eg

    Since I have a free license for windows 7 Professional at home, I decided to install it on my new Pavilion dv6-6c45eg that came with Windows 7 Home Premium. The problem is that the installation process stops at the beginning and said something like: