Choose field from the joined table based on another field?

Hello!

Is it possible to select a field in a row of attached table based on another field? Consider the following data in the CVALUES table:
ID  C1   C2    C3    C4    C5    C6
T1  9.6  10.3  12.4  19.6  19.6    0
T7  9.6  10.4     0     0     0    0
T3  8.2   9.9  11.4  19.6  19.6    0
T8  8.3  10.7  13.5  20.7     0    0
T5  9.4  10.3  12.7  22.9  19.6    0
...
And the following data in the table MDETAIL:
ID           MDATE        CNUM     ...
T1  03/28/2012 18:00:00    4
T1  03/28/2012 18:00:00    1
T8  03/29/2012 06:00:00    6
T8  03/29/2012 06:00:00    5
...
I want to get the column corresponding to the VALUE field in the table MDETAIL CVALUES. I'm doing this:
SELECT m.t1, m.mdate,
       CASE WHEN m.cnum = 1 THEN c.c1
            WHEN m.cnum = 2 THEN c.c2
            WHEN m.cnum = 3 THEN c.c3
            WHEN m.cnum = 4 THEN c.c4
            WHEN m.cnum = 5 THEN c.c5
            WHEN m.cnum = 6 THEN c.c6 END AS cvalue
  FROM mdetail m
  JOIN cvalues c ON m.id = c.id
The CVALUES table records are unique; only one line by ID.

This method works and it's fast, but it's messy code and I think there must be a better way to do it. I use cvalue value in several calculations so each calculation that I use is this giant glob of CASES inside!

Hello

With a decoding it would be easier, but as ugly :-)

SELECT m.t1, m.mdate,
       DECODE( m.cnum , 1 , c.c1, 2, c.c2, 3, c.c3, 4, c.c4, 5, c,c5, 6, c,c6) cvalue
  FROM mdetail m, cvalues c
  WHERE m.id = c.id

Success!

FJFranken

Tags: Database

Similar Questions

  • From the versioned tables link to another diagram

    Hello!

    We are currently assessing the mechanisms of synchronization of data as possible for a future database that must use data from a database of existing B.
    option 1 is the simplest: export data from B, import in A and you're done. the problems are of course used those like arguing with Exim, how up-to-date the data is, etc.
    However, we are more interested in option 2: link to the tables of a B. If we are not wrong here we could establish this with the help of dblink and synonymous.
    done importand is arrays in B are versioned with OWM. is it in any way possible to link to those who in this case? and if so, how selection of work/savepoint space be treated?
    We suspect that this is not possible, at least not if A base table are not versioned as well. or you would need to omit true foreign keys when the referencing of the versioned tables in B of A, which is not exactly good either.

    Thanks for the advice in advance!

    Kind regards

    Andreas

    Hi Andreas,

    You can use synonyms and links. GotoWorkspace works the same as it would for the local database. Just run it on the remote database.

    For example,.
    SQL > exec dbms_wm. GotoWorkspace@dblink ('Workspace1');

    You should be able to use tables from different databases in the same query. However, it is not any child support for foreign key constraints where the tables are set to different databases and only the tables is transferred. In the case, the parent table must be part of the same db as the child.

    Kind regards
    Ben

  • How do I display values skyrocket in the IR filter for the joined table columns?

    Hello

    I have a problem in the IR the query is based on a table, joined with other tables. I would like to provide users the ability to use IR filter search bar in the joined table columns. The problem facing on this filter, the Expression field, by pressing the arrow button displays values for the fields in the primary table, but not for fields that come from joined tables. Have you experienced this behavior in your reports? Is this normal?

    TIA

    Hello

    Correlated subqueries can improve performance - but it does not depend on the involved tables, the number of columns and the existence of indices. As far as I know, the optimizer has problems with them. You could try to explain the Plans on the two statements to verify that.

    In any case, I created a new test page with the SQL for IR:

    SELECT E.EMPNO,
    E.ENAME,
    D.DEPTNO,
    D.DNAME,
    E2.EMPNO "EMPNO2",
    E2.ENAME "ENAME2"
    FROM EMP E, EMP2 E2, DEPT D
    WHERE E.EMPNO = E2.EMPNO(+)
    AND E.DEPTNO = D.DEPTNO(+)
    AND E2.PRIMARY_EMPLOYEE(+) = 'Y'
    

    http://Apex.Oracle.com/pls/OTN/f?p=267:226

    As far as I can see, it works properly - except that if I do a filter on the ename column, when I try to create a second filter, drop-down lists ename all the values, while the other columns list only the available values after having applied the first filter. Which seems strange that the filters are applied as ANDS. But it does the same thing for other areas - IE, the field used in a filter is not filtered for the second filter - so I guess this is normal, but only a person in Apex could probably explain why it is so.

    Otherwise, everything seems to work as I expect and the above page works the same as my test page, which uses external joins http://apex.oracle.com/pls/otn/f?p=267:224

    Andy

  • Need to know the grain of the join table

    Hi Experts,

    I have a doubt,

    Let say I take data from two tables
    pay_costs
    PAY_COST_ALLOCATIONS_F

    for tables, the columns are
    pay_costs. BALANCE_OR_COSTS
    pay_costs. DEBIT_OR_CREDIT

    PAY_COST_ALLOCATIONS_F.PROPORTION

    Now, I need to join these two tables respectively. I followed site etrm and found that we can create the join

    PAY_COST_ALLOCATIONS_F.PAY_COST_ALLOCATION_KEYFLEX = PAY_COST_ALLOCATION_KEYFLEX. PAY_COST_ALLOCATION_KEYFLEX
    and PAY_COST_ALLOCATION_KEYFLEX. COST_ALLOCATION_KEYFLEX_ID = pay_costs. COST_ALLOCATION_KEYFLEX_ID

    so I'm doing something wrong. The grain of the data is going to change... Help kindly I am new to the field of ebs

    Hello
    You will potentially have millions of records in PAY_COSTS because it's the main compensation table.

    Your PAY_COST_ALLOCATIONS_F contains only a breakdown of how the costs of a user should be broken out. There is an ASSIGNMENT_ID (which connects to the person) and a PROPORTION corresponding to the percentage.

    Depending on how long the person has been with the company, for each assignment ID (that you can consider PERSON_ID), there might be hundreds of files in PAY_COSTS.

    The primary key of the PAY_COSTS is the COST_ID while the primary key of the PAY_COST_ALLOCATIONS_F is COST_ALLOCATION_ID more EFFECTIVE BEGIN and EFFECTIVE END dates. Which tells me, is that the benefits are time and if you try and join the ALLOCATION_ID you will end up with a wrong answer. If you were within the E-Business Suite, application effective date using the current date as the default value. Then, it does is apply a filter on all tables that have dates of application. Thus, for example, you may have to limit benefits to know those that this could be done using this SQL:

    SELECT
    ASSIGNMENT_ID, COST_ALLOCATION_KEYFLEX_ID, PROPORTION
    Of
    HR. PAY_COST_ALLOCATIONS_F
    WHERE
    SYSDATE BETWEEN EFFECTIVE_START_DATE AND EFFECTIVE_END_DATE
    ORDER OF ASSIGNMENT_ID, COST_ALLOCATION_KEYFLEX_ID

    You can get a clue by COUNT (*) from the two tables. If there are more records from a table that another so you can be sure that there is not a join one of 1:1 and therefore the granularity must be 1: n one-to-many

    Hope this helps
    Michael

  • Update of the table based on another

    Hello

    I'm trying to update a date column in a table based on another table. There is a unique ID in each table that identifies and connects each person:
    update patient_bu a set a.entry_date = (select b.entry_date from CLIENT_MED_DT b where a.id = b.id)
    This is the error:

    ORA-01427: einreihig subquery returns multiple rows


    The problem (I think) is that the select statement back several dates for records people. There are some people / records that have multiple entry dates, but I'm only interested in the most recent date.

    How can I update this table with only the most recent date?

    Thanks for any help,
    Matt

    Oh well, I forgot the UPDATE clause:

    SQL> create table patient_bu
      2  (id number
      3  ,entry_date date);
    
    Table created.
    
    SQL> create table client_met_dt
      2  (id number
      3  ,entry_date date);
    
    Table created.
    
    SQL> merge into patient_bu a
      2  using (select id
      3               ,max(entry_date) max_date
      4         from   client_met_dt
      5         group  by id
      6        ) b
      7  on    (a.id = b.id)
      8  when  matched then update
      9        set a.entry_date = b.max_date;
    
    0 rows merged.
    
  • How to update the password on the other table based on the mod

    Hello

    I need to update the password for the other table based on the mod.

    I have two table 'user' and 'reset '.


    User:

    Username username password date_exp

    s232322 Bobby John sdds4545df4r 10/12/2010
    s454566 George Bill f444556gh76 07/14/2010
    s343466 Bill Clinton fgf54tyujkkkhj 06/12/2011
    .
    .
    .


    Reset:

    Series encrypt decrypt

    fgf5656fg 0 Watellemon
    1 dfggf5665 lime juice
    dfdffd545 2 applesalad
    3 54r4gggt airlines
    construction of 4 hg44hh63
    5 fgf4343yh security
    .
    .
    .

    How can I update the password of a table to another table "reset"?

    Ex: query

    Update user set password = (select Encrypt reset where mod (length (username), 10))


    If mod (length (username), 10) is 0, it should take series "0" encrypt the password and update the password in the table "user".
    If mod (length (username), 10) is 1, it should take serial '1' encrypt the password and update the password in the table "user".
    If mod (length (username), 10) is 2, it should take series '2' encrypt the password and update the password in the table "user".
    If mod (length (username), 10) is 3, it should take series '3' encrypt the password and update the password in the table "user".
    If mod (length (username), 10) is 4, it should take series '4' encrypt the password and update the password in the table "user".
    ....


    Please help me write an update query.




    Thank you
    Nihar

    Hi, Nihar,

    You almost had it! You just need to correlate the subquery for the main UPDATE statement, like this:

    UPDATE     user_table     u     -- USER is a built-in function; it's not a good table name
    SET     password = ( SELECT  encrypt
                   FROM    reset     -- RESET isn't a very good table name, either
                   WHERE   serial     = MOD ( LENGTH (u.username)
                                             , 10
                                   )
                 );
    
  • ORA-22992: cannot use LOB Locators selected from the remote tables...

    Oracle 10.2.1.0.4
    Solaris 10

    We try to access a table in another database via a db_link.

    The table we are trying to access has a LOB.

    We get the following error: ora-22992: cannot use LOB Locators selected from the remote tables.

    Is there a way to get around this? We need the data in the BLOB field.

    Thank you.

    See on metalink:

    ORA-22992 has a workaround solution in 10 gr 2
    DOC - ID: 436707.1

    Werner

  • Units (Windows 7) media center impossible compleate the next option every time that I "click" or choose it from the menu option within the center of Meduia

    Whenever I "click" or choose it from the options menu in the center of Meduia units (Windows 7) media center impossible compleate the next option.

    Whenever I select the option, "more", all I get is a splashbox that says simply, "" INVALID APPLICATION, cannot start. "" This option is located in the general Menu of the Media Center in the submenu 'tasks' .

    Thank you...

    Run the Setup again.

    Open WMC > tasks > settings > General > installation Windows Media Center > rerun the installation

    Does that help?

    If the above does not help then try to reset WMC: -.

    Open Control Panel > uninstall a program > turn Windows features on or off > scroll multimedia features > uncheck the box against Windows Media Center > follow the instructions on the screen > Reboot > open again to turn Windows features on or off in the control panel > scroll multimedia features > check the box against Windows Media Center.

    Now reconfigure WMC again.

  • XML from the Oracle Table data

    Hi all

    I'm new to this network. I'm also new to oracle XML package. I want a help with the query below.

    CREATE TABLE EMP (ID NUMBER PRIMARY KEY, NAME VARCHAR2 (10), TELEPHONE NUMBER);

    INSERT INTO EMP (ID, NAME, PHONE) VALUES (11, 'Joy', 1234);
    INSERT INTO EMP (ID, NAME, PHONE) VALUES (22, 'Mike', 5678).
    INSERT INTO EMP (ID, NAME, PHONE) VALUES (33, "Jason", NULL);
    COMMIT;

    I want to export data from the EMP table in an XML file with the format below.

    Power required:

    <? XML version = "1.0" encoding = "UTF-8"? > < Joy STATICDATA > < EMP > < ID > 11 < /ID > < NAME > < / NAME > < / EMP > < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? > < Mike STATICDATA > < EMP > < ID > 22 < /ID > < NAME > < / NAME > < / EMP > < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? > < Jason STATICDATA > < EMP > < ID > 33 < /ID > < NAME > < / NAME > < / EMP > < / STATICDATA >

    I used some XML functions and you wrote the following query.

    Select XMLROOT (XMLELEMENT (staticdata, XMLELEMENT (EMP, XMLELEMENT(ID,ID), XMLELEMENT(NAME,NAME))), version "1.0" encoding = "UTF - 8') xml EMP;

    my query output:

    <? XML version = "1.0" encoding = "UTF-8"? >
    < STATICDATA >
    < EMP >
    < ID > 11 / < ID >
    Joy of < NAME > < / NAME >
    < / EMP >
    < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? >
    < STATICDATA >
    < EMP >
    < ID > 22 / < ID >
    < NAME > Mike < / NAME >
    < / EMP >
    < / STATICDATA >
    <? XML version = "1.0" encoding = "UTF-8"? >
    < STATICDATA >
    < EMP >
    < ID > 33 / < ID >
    Jason < NAME > < / NAME >
    < / EMP >
    < / STATICDATA >


    But I want the out as the power required above. all records in a single line. can someone help me achieve the desired output. also can I export all columns of the table with something like select * from the table in the XML file?

    Thank you
    Delobelle

    Don't know why you need it on a single line, but you could:

    Select XMLTYPE (REGEXP_REPLACE (XMLROOT (XMLELEMENT (staticdata, XMLELEMENT (EMP, XMLELEMENT(ID,ID), XMLELEMENT(NAME,NAME))), version "1.0" encoding = "UTF - 8'), CHR (10) |)) ' *<><'))>
    FROM EMP;

    SY.

  • Select values from the db1 table and insert into the DB2 table

    Hello

    I have three databases oracle running in three different machines. their ip address is different. among the DB can access databases. (means am able to select values and insert values into tables individually.)

    I need to extract data from the DB1 table (ip say DB1 is 10.10.10.10 and the user is DB1user and the table is DB1user_table) and insert the values into DB2 table (say ip DB2 is 11.11.11.11 and the user is DB2user and table DB2user_table) of DB3 that is to have access to the two IPs DB.

    How do I do this

    Edited by: Aemunathan on February 10, 2010 23:12

    Depending on the amount of data must be moved between DB1 and DB2, and the frequency at which this should happen, you might consider the SQL * COPY more control. I think it's very useful for one-off tasks little, so I can live within its limits of the data type. More http://download.oracle.com/docs/cd/E11882_01/server.112/e10823/apb.htm#i641251.

    Change some parameter of sqlplus session are almost mandatory in order to get decent transfer rates. Tuning ARRAYSIZE and COPYCOMMIT can make a huge difference in flow. LONG change may be necessary, too, depending on your data. The documentation offers these notes on use:

    To activate the copy of data between Oracle and databases non-Oracle, NUMBER of columns is replaced by DECIMAL columns in the destination table. Therefore, if you are copying between Oracle databases, a NUMBER column with no precision will become a DECIMAL column (38). When copying between Oracle databases, you must use SQL commands (CREATE TABLE AS and INSERTION), or you must make sure that your columns have a specified precision.

    SQL * the VALUE LONGER variable limits the length of the LONG column you are copying. If all LONG columns contain data exceeds the value of LONG, COPY truncates the data.

    SQL * Plus performs a validation at the end of each successful COPY. If you set the SQL * variable more COPYCOMMIT DEFINED to a value positive n, SQL * Plus performs a validation after copying all lots n of records. The SQL * Plus ARRAYSIZE variable SET determines the size of a batch.

    Some operating environments require that the service names be placed between double quotes.

    From a SQL * Plus term on DB3, can resemble the command to move all content from my_table in DB1 to the same table in DB2

    COPY from user1/pass1@DB1 to user2/pass2@DB2 -
    INSERT INTO my_table -
    USING select * from my_table
    

    Note the SQL code * more line-continuation character ' - '. It is used to escape the newline character in a SQL * Plus command if you do not have to type all on one line. I use it all the time with this command, but I can't locate the documentation on that right now. Maybe someone else can put their finger on it.

    There are other ways to accomplish what the command copy and it is not without its quirks and limitations, but I find that there is usefulness in an Oracle Toolbox.

  • How can I get the second and third highest salary from the emp table

    How can I get the second and third highest salary from the emp table in the ecah Department
    SQL> ed
    Wrote file afiedt.buf
    
      1  select empno, ename, sal, deptno
      2  from (
      3    select empno, ename, sal, deptno, dense_rank() over (partition by deptno order by sal desc) as rn
      4    from emp
      5    )
      6* where rn in (2,3)
    SQL> /
    
         EMPNO ENAME             SAL     DEPTNO
    ---------- ---------- ---------- ----------
          7782 CLARK            2450         10
          7934 MILLER           1300         10
          7566 JONES            2975         20
          7876 ADAMS            1100         20
          7499 ALLEN            1600         30
          7844 TURNER           1500         30
    
    6 rows selected.
    
    SQL>
    
  • best way to create a table based on another table

    Hello
    I am trying to create a table based on another table with all the data in it. It contains important data.

    create table < tablename > select * from table1.

    Is the best way to do it, or is there another way. Please advice.

    Thank you

    Insert / * + append * / in as select * from ;

    It should be->

    insert /*+ append */ into 
    select * from ;
    

    Kind regards.

    LOULOU.

  • loop chart - the chart is based on another query

    I'm working on a report of BI Publisher. The report displays information for a list of stores (store group, by store - each store page break is a page). Now, we want to add a chart (weekly sales - group per week) for each store, we know there is "table within the group" box, we can use, but our problem is that the chart is based on another query, not the original group by query parent store. Could you please help? Detailed information can be provided if required.

    Please check your Inbox.
    I sent you the answer.

    See you soon

    Jorge
    p.s if this answers your question please give the points and close the message

  • Insert data into the same table based on certain conditions

    Hello. I'm new to this forum.
    I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:

    Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
    I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:

    1. if PaidAmount > 0 & & CurrentInstallment < TotalInstallment then

    CurrentInstallment = CurrentInstallment + 1

    2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...

    So, I have to insert data by incrementing the month and year. for example:

    If currentdata is 11/2012 next data will be 12/2012

    But following will be 01, 2013
    I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit on each record selected and insert data and then turns them into table (MYTABLE) even.

    How to achieve that?

    Thank you.

    978184 wrote:
    Hello. I'm new to this forum.
    I have to write a stored procedure to insert data in a table MYTABLE say, having a structure like:

    Col1 Col2 Col3... TotalInstallments CurrentInstallment PaidAmount MonthYear
    I have to insert all the data that it is in the same table (MYTABLE) except change some fields based on certain conditions:

    1. if PaidAmount > 0 & CurrentInstallment

    CurrentInstallment = CurrentInstallment + 1

    2. in the field MonthYear I have data ex. 01/2012, 11/2012 formate(month/year)...

    So, I have to insert data by incrementing the month and year. for example:

    If currentdata is 11/2012 next data will be 12/2012

    But following will be 01, 2013
    I have to select all records that belongs to the previous month (across the field MonthYear) and put the audit & on each of the selected data record and insert then turns them into table (MYTABLE).

    You can do this way:

    This is not tested, but if you can provide the example of table structure and data (IN create table and insert scripts), it can be put to the test.

    insert into your_table
    (col1, col2, col3...current_installment, month_field)
    select col1, col2, col3..,
           current_installment +
           case when paidamount > 0 and current_installment < total_installment then
            row_number() over (
                                partition by column1, column2,.. columnn      -->You may choose partition if you want the
                                                                              --Increment of Current_installment to reset after particular combination ends
                                order by primary_key        -->Order the Increment, you may choose to add more columns to order by
                              )
          else
            0                                               --> if condition is not met, then Add 0
          end curr_installment,
          add_months(to_date(month_field, 'MM/YYYY'), 1) nxt_month
      from your_table;
    
  • Display the value of the column automatically based on another column in a table

    Hello

    I use APEX 5.0, I have a requirement to display the field automatically based on the tabular presentation of other numbers.

    img.png

    in the screen shot attached above when I press Add button line a new line is created and enter the value in 'place no.', 'dept and identification of height place' when I press tab or get out of the column "height", "GID" should appear automatically.

    To display the GID my query will be like that. "Select GID from x where no = '1011' place and dept = 88 and height = 88'.

    How to get the value of from GID of the another value entered in Place no, dept and height in tabular form.

    Thanks in advance

    Ravi.

    Hi Ravi

    No need to point of Application or process. I just completed the task with the following steps

    (1) create 4 hidden items

    P2_SELWID

    P2_SELDEPTH

    P2_SELHEIGHT

    P2_CALCWNAME

    (2) gives 3 CSS classes in three columns of tables.

    . WID

    . DEPTH

    . HEIGHT

    (3) create the function jQuery for re - get three items from the page on a line if one of them has been changed.

    Add the following code to the function and the declaration of global variables

    function setRowItemValues(p_rownum){
        wid_col_id = 'f02_' + rownum ;
        wid_col_val = $('#'+wid_col_id).val();
        depth_col_id = 'f03_' + rownum ;
        depth_col_val = $('#'+depth_col_id).val();
        height_col_id = 'f04_' + rownum;
        height_col_val = $('#'+height_col_id).val();
        wname_col_id = 'f05_' + rownum;
        // set P2_SELWID,P2_SELDEPTH,P2_SELHEIGHT
        $s("P2_SELWID", wid_col_val );
        $s("P2_SELDEPTH", depth_col_val );
        $s("P2_SELHEIGHT", height_col_val );
        $s("P2_SELWNAMEID", wname_col_id );
        // Call the Dynamic Action to get WNAME if all values are not null
        if( wid_col_val != ''  && depth_col_val != '' && height_col_val != '' ){
            //Call DA to Get WNAME from WINT
            $.event.trigger("CalcWNAME");
            // set the Tabular form
            $('#'+wname_col_id).val($x("P2_CALCWNAME").value);
        }
    }
    

    and run when the Page loads

    $(document).on("change", ".WID,.DEPTH,.HEIGHT", function(){
        rownum = $(this).attr('id').substr(4);
        setRowItemValues(rownum);
    });
    

    (4) create a dynamic Action to get WNAME based on a PL/SQL function. Note the submitted page elements.

    CalcWNAME

    (5) set of the corresponding element in the line of the function jQuery, after completing the DA.

    Please check and I hope your problem is now solved.

    Concerning

    Mahmoud

Maybe you are looking for