Insert/update of the individual fields in the DB Table

So maybe I have a few noble desires, but I have my trying to find a way to change 1 single record (field) in a table.

SCENARIO: I want to have a table that has the columns 'cid', 'class_name' and 'open_seats '... For example, if you have a line with values:

1, namemw520, then a second, 25'

2, nametr520, 32', and a third,.

etc...

Then in a form when the person fits and chooses a class, into submission, he will subtract one from the open_seats column and affect only the row of the selected class_name.

WHAT I TRIED:

So far, the only thing I can think is to have several queries to several tables in a line, and then update a form based on a selection of drop-down list doesn't seem to work, in addition, there are like 30 classes and I don't want to create tables of 30 DB, if it can be avoided. Or I'm WAY over my head on this one?

Any help would be appreciated.

TIA

A more standardized design of database win the obligation to do something.

Consider a design where your table has a field of ability instead of a seats_remaining.  Once you have configured a class, you should not change this value.  Then, as people sign up, you fill in a many-to-table number.  I hope that you are already doing.  Then to see how many points you have left, you do something like this:

Select the capacity-

(select count (*)

your many at table number

where is this class) seats_remaining

your table class

where is this class

Tags: ColdFusion

Similar Questions

  • Insert newspapers in the errors table

    Hello

    I want to insert errors in the custom error message table.
    eventhogh is runs properly but I cannot insert rows in the error table.

    This is the code.

    DECLARE
    v_status VARCHAR2 (20);
    cursor c1
    is
    Select * from all_errors
    When type = 'PROCEDURE '.
    and name = upper ('updatecourse');
    v_rec c1% rowtype;
    BEGIN
    Select the State
    in v_status
    of object
    where object_type = upper ('PROCEDURE')
    and object_name = upper ('updatecourse');
    If v_status = "INVALID" Then
    Open c1;
    loop
    Fetch c1 in v_rec;
    When exit c1% notfound;
    insert into values of error_log (owner, name, type, text) (v_rec.owner, v_rec.name, v_rec.type, v_rec.text);
    end loop;
    Close c1;
    end if;
    END;

    Please let me know what to do.

    Thank you
    Rami Reddy.

    Why a loop?

    You can manage in a simple SQL statement... (Not tested, just to give an idea). I hope that you don't have the name of this procedure in the two schemas

    insert into error_log(owner,name,type,text)
    select owner,name,type,text
    from all_errors
    where type='PROCEDURE'
    and name=upper('updatecourse')
    and 'INVALID' =
       (
       select o.status
       from all_objects o
       where o.object_type=upper('PROCEDURE')
       and o.object_name=upper('updatecourse');
       );
    
  • How to capture information on what program is inserting/updating of the data in the table?

    Hello


    I'm working on an application where we have an ABC chart that has a STATUS column. This column is updated from somewhere.

    However, we are unable to determine where this COLUMN is updated. I checked the DBA_SOURCE table to see if this table is updated from any oracle proc / function. But isn't.

    Is there anyway that I can identify what program will update this table? I can create a before update trigger but I'm not sure if that I can capture program (shell script / informatica workflow) that is updated in this table.

    You can use in a trigger SYS_CONTEXT 'USERENV' information ' MODULE'

    create table mytable
    (
      col1   VARCHAR2(10)
    , col2   VARCHAR2(50)
    );
    
    create or replace trigger mytable_bi_trigger
    before insert
    on mytable
    for each row
    begin
    
        :new.col2 := SYS_CONTEXT('USERENV','MODULE');
    
    end;
    /
    
    -- from Toad
    insert into mytable(col1) values('Test 1');
    commit;
    -- From sqlplus
    insert into mytable(col1) values('Test 2');
    commit;
    
    select * from mytable;
    
    COL1       COL2
    ---------- --------------------------------------------------
    Test 1     TOAD 11.0.0.116
    Test 2     SQL*Plus        
    

    You can also capture a lot more information (host name of the client, the os user, etc.).

    See http://docs.oracle.com/database/121/SQLRF/functions199.htm#SQLRF06117

    Kind regards.

    Alberto

  • Insert/update of the lines in tabular form from one table to another table

    Hello

    I'm having a tabular presentation for a table (equip_test) with 2 columns (equip_id, equip_name). I create a new

    Button (submit_alt) in this tabular form. I need to insert or update lines that are changed in this table to form

    another table (equip_staging) when you press the submit_alt button. How can I do this? How can I identify which lines are

    Insert or update? The process of the page I am trying since this button is
    begin
    
    FOR i in 1..APEX_APPLICATION.G_F01.count
    
    LOOP
    insert into equip_staging values(APEX_APPLICATION.G_F02(i),APEX_APPLICATION.G_F03(i));
    END LOOP;
    
    end;
    But it does not work. Help, please

    Thank you

    TJ

    Unchecking (does not) the column is a problem. Use this SQL instead (which is included in my examples):

    SELECT empno,
              empno
           || apex_item.hidden (33, wwv_flow_item.md5 (ename, sal, job))
                                                                    empno_display,
           ename, sal, job
      FROM emp
    

    and join this column hidden from your display column. In this way, it should work. When you feel there is no data found message in the treatment in a table, it will mean that the id does not exist.

    Denes Kubicek
    ------------------------------------------------------------------------------
    http://deneskubicek.blogspot.com/
    http://www.Opal-consulting.de/training
    http://Apex.Oracle.com/pls/OTN/f?p=31517:1
    ------------------------------------------------------------------------------

  • Restrict the insert, update, delete the other user table

    I created a new database user. I have granted the privileges system into a TOAD.

    • Select any table
    • alter any table
    • delete a table

    I found that account can select, alter, drop table arrays of other schema. So how I can limit the which account to modify, remove only its tables belonging and select table of all the schema?

    Is there the ALTER table, ALTER view, ALTER procedure, ALTER synonym, DROP table, DROP view, DROP procedure, DROP synonym system privileges? How can I grant it without ANY system privileges?

  • 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;
    
  • Insert records in the W_EXCH_RATE_G table.

    Hi all

    Can someone help me with the following scenario.

    I would like to know how to insert additional record of exchange rates in the W_EXCH_RATE_G table in the data warehouse.
    I checked records in this table ate PS_RT_RATE_TBL of peoplesoft.
    But I have just a flat file and I have to complete additional registration.
    Can you let me know if I have to use and update the query to do the same thing or do I change the mapping in Informatica?

    Thanks for the help in advance!
    -Nikki

    Nikki, see also my response to your similar message: -.

    Change SIL_GLBalanceFact to populate the Balance_Global1_Amt column.

    Please indicate if helpful/useful.
    Andy.

  • Insert select on the same table: possible without side effects?

    I have a very large table T1 containing millions of records. I need to treat its lines and create a few new lines based on selection.

    Table T1 contains events and one of them, with the code 100, is created by the further development of other events inside the table.

    My code is as follows:

    insert /*+append */ into T1 (code,...) values (100, c1,c2,...)
    select c1,c2... from T1 where (code=20 or code=10) and <other conditions>...
    
    

    as you can see I'm extract T1 lines to insert again in T1 with a different code and I use the direct path in order to get good performance.

    My fear is: choose is made from the same table I risk data loss? In general it is a good practice? Or is it better to create another table?

    Hello

    No I don't think that there may be loss of data. But that may depend on the behavior of the application and your where clause.

    I will explain how it is treated, so you can see if it's ok:

    1. the table is locked because of the insert add, (as)

    2 lines are read by select and make compatible from the State that was at the beginning of the query - 1.

    3 rows are inserted at the end, after the high-water line

    4 columns for new lines are sorted to be merged in the index

    5. high watermark is adjusted - visible new lines and lock is released

    Note that 2. and 3. occur at the same time: rows are inserted all read.

    Note that anyone can choose in the table during the operation - they see the changes committed only - if the State 1.

    all other DML are waiting for the lock being released, and will see new ranks and then

    If you have things that prevent the direct-path insert, append the hint will be ignored. So, if you must rely on close to 1. then the best lock explicitly with table lock. But I don't think that you need.

    Kind regards

    Franck.

  • Updating of the ADF Table strangely after delete line

    Hello

    I use ADF Table with ArrayList to the model of the collection. I do not use View object

    < af:table var = value ' row' = "#{pageFlowScope.dataBean.collectionModel}"... "

    I have a SelectOnceChoice in each row of the table and bind their values to a property of the line

    < af:selectOneChoice value = "#{row.label}"... "

    I have a command link to remove each line and the delete method:

    collectionModel.remove (idx);

    The model is correctly updated, but the table of the ADF is not. Somehow, the value of the selection of the next line is set to the value of the deleted row.

    For example: If there two select with values respectively: Foo, Bar. If I remove the line with Foo, the remaining line (initially Bar) is set to Foo.

    If I refresh (F5) the page, it appears correctly. I also tried to use addPartialTarget and partialTriggers but no help.

    Can someone help me? Thanks in advance.

    Post edited by: 2925664

    Hello

    Previously I use table binding, that's why it has that name.

    BTW, I don't know what's causing the problem. It is related to the life cycle ADF. I put the immediate = "true" for the select box and it is working fine now.

    Concerning

  • UPDATE on the same table in the sub query

    DB version: 11.2

    We have a table called SHP_GC_TRACK, which has about 8 million records with partitions. In the below UPDATE, it updates a column based on the SELECT on the same table in a subquery.
    UPDATE shp_gc_track a
       SET f_tran_proc  = 'Y'
     WHERE last_update_date <
              (SELECT MAX (last_update_date)
                 FROM shp_gc_track b
                WHERE a.shp_trx_rowid = b.shp_trx_rowid
                  AND a.c_shp_inst = b.c_shp_inst
                  AND a.f_tran_proc  = b.f_tran_proc 
                  AND b.f_ltr_received = 'D'
                  AND f_rec_code IN ('G', 'W')
                  AND b.f_rec_status = 'B'
                  AND b.c_shp_inst = :b1
                  )
       AND a.c_shp_inst = :b1
       AND a.f_ltr_received = 'D'             -----------------> part of composite index
       AND a.f_tran_proc  = 'N'              -----------------> part of composite index
       AND a.f_rec_code IN ('G', 'W')      --------------> part of composite index  
       AND a.f_rec_status = 'B';              -----------------> part of composite index  
    This UPDATE takes a long time to run and sometimes get hung up.

    We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status and f_tran_proc. Explain the plan shows that this composite index is used.



    Any way to rewrite this query or suggestion?

    Steve_74 wrote:
    DB version: 11.2

    We have a table called SHP_GC_TRACK, which has about 8 million records with partitions. In the below UPDATE, it updates a column based on the SELECT on the same table in a subquery.

    UPDATE shp_gc_track a
    SET f_tran_proc  = 'Y'
    WHERE last_update_date <
    (SELECT MAX (last_update_date)
    FROM shp_gc_track b
    WHERE a.shp_trx_rowid = b.shp_trx_rowid
    AND a.c_shp_inst = b.c_shp_inst
    AND a.f_tran_proc  = b.f_tran_proc
    AND b.f_ltr_received = 'D'
    AND f_rec_code IN ('G', 'W')
    AND b.f_rec_status = 'B'
    AND b.c_shp_inst = :b1
    )
    AND a.c_shp_inst = :b1
    AND a.f_ltr_received = 'D'             -----------------> part of composite index
    AND a.f_tran_proc  = 'N'              -----------------> part of composite index
    AND a.f_rec_code IN ('G', 'W')      --------------> part of composite index
    AND a.f_rec_status = 'B';              -----------------> part of composite index  
    

    This UPDATE takes a long time to run and sometimes get hung up.

    We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status and f_tran_proc. Explain the plan shows that this composite index is used.

    Any way to rewrite this query or suggestion?

    Setting updates with subqueries may be difficult: (.) Unfortunately my suggestions below are of the try-it-and-see-what-happens variety - nothing of certain

    First of all, check the index. Is it bitmap or tree? If the tree to see if the more restrictive columns are listed first - this can help with effectiveness of b-tree indexes. Also if the tree a composite bitmap for columns with lots of repeat values instead could help

    Its a correlated subquery so that you cannot run just the subquery first put the result in a scalar varaiable and using the variable in the SQL instead. You can try putting the keys w/join subuqery results in a TWG first to use TWG in SQL to see if I/O is reduced together during these two operations.

    You have the license for the parallel query option? Using parallel DML (this must be activated manually) can help. Check the documentation of the ALTER SESSION command to do so. In addition, the PARALLEL_INDEX() indicator could help

    Display the SQL execution plan

  • Lack of updating of the secondary table.

    JDeveloper 11.1.1.20.

    In a master page in detail (two ADF unalterable tables) where the detail listen to changes in the master with its partialtrigger set to the main table, I discovered a problem when I go back to the page of a RESTORE operation in another page (change).
    In doing so, sends me to the first line of the main table, which is normal.

    -The detail section says "No data to display", which is not correct.
    If I leave the first row and return to the detail section is updated.

    Any success?

    Best regards
    Erik

    What is the RefreshCondition that you have defined for the iterator?
    You can change it to ifNeeded or always and check if it helps?

    Thank you
    Nini

  • update of the EUL5 Tables

    Hello

    I need to rename my files EUL & elements following a naming convention, I thought updating the tables of EUL5 in the following way:


    -Step 1: Create a copy of eul5_objs:
    create table eul5_objs_cpy as select * from eul5_objs ;
    -Step 2: update eul5_objs:
    update eul5_objs set obj_name = initcap (obj_name);
    -Step 3: Create a copy of eul5_expressions:
    create table eul5_expressions_cpy as select * from eul5_ expressions ;
    -Step 4: Update the eul5_ expressions:
    UPDATE eul5_expressions e
       SET e.exp_name =
           (SELECT TRIM(DECODE(INSTR(m.exp_name, ':'),
                          0,
                          LOWER(t.obj_name) || ' : ' || INITCAP(m.exp_name),
                          SUBSTR(m.exp_name, 1, INSTR(m.exp_name, ':', 1, 1) - 1) ||
                          INITCAP(SUBSTR(m.exp_name, INSTR(m.exp_name, ':', 1, 1)))))
              FROM eul5_expressions m, EUL5_OBJS t
             WHERE m.it_obj_id = t.obj_id
               AND e.exp_id = m.exp_id)
     WHERE e.it_obj_id is not null
    but I heard that EUL5 tables updated could corrupt the EUL and he'd end up unable to export/import it via the command line, then asked me to update the EUL manually by the administrator.

    For my information, could someone explein me what is the difference and how it could damage the EUL?

    Thank you.

    Mcha

    Hi mcha
    Like the previous of Tamir answer, you should be careful updating behind the scenes. According to me, these are the only paintings directly referencing the name in this case, it seems that you should be ok.

    One thing to be aware of, is that workbooks also incorporate the name and identifier of the folder within each compiled worksheet. As long as the administrator does not change the name and the identifier at the same time binders will continue to open. However, until the workbook is re-registered only the identifier will be correct and should anyone subsequently change an identifier without a workbook saved shows the name of the folder then change the workbook will open more.

    Therefore, I advise you also ask your users to open each workbook after the name change and then re-register. In this way, the new name of the folder will get embedded in spreadsheets, sort of covering all the bases.

    Best wishes
    Michael

  • cannot insert data into the PRODUCT_USER_PROFILE table

    I've connected to the database as the sysdba, which is installed on VMWARE. database is oracle 11g.
    whenever I insert data in the table PRODUCT_USER_PROFILE that the database returns 1 row inserted and then when I try to show everything on the table before or after the statement commit is made the database returns "No. LINES SELECTED.

    guys any idea about this problem...

    Hello

    Try to connect as a system and make the insert and check. Always think about the issue.

    -Pavan Kumar N
    Oracle 9i / 10g - OCP
    http://oracleinternals.blogspot.com/

    Published by: pounet on January 4, 2010 16:29

  • Add more than 2 lines for a select statement without inserting rows in the base table

    Hi all

    I have a below a simple select statement that is querying a table.

    Select * from STUDY_SCHED_INTERVAL_TEMP
    where STUDY_KEY = 1063;

    but here's the situation. As you can see its return 7 ranks. But I must add
    2 rows more... with everything else, default or what exist... except the adding more than 2 lines.
    I can't insert in the base table. I want my results to end incrementing by 2 days in
    measurement_date_Taken on 01-APR-09... so big measurement_date_taken expected to
    end at study_end_Date...



    IS IT STILL POSSIBLE WITHOUT INSERT ROWS IN THE TABLE AND PLAYIHY ALL AROUND WITH
    THE SELECT STATEMENT?

    Sorry if this is confusing... I'm on 10.2.0.3

    Published by: S2K on August 13, 2009 14:19

    Well, I don't know if this request is as beautiful as my lawn, but seems to work even when ;)
    I used the "simplified" version, but the principle should work for your table, S2K.
    As Frank has already pointed out (and I fell on it while clunging): simply select your already existing lines and union them with the 'missing documents', you calculate the number of days that you are "missing" based on the study_end_date:

    MHO%xe> alter session set nls_date_language='AMERICAN';
    
    Sessie is gewijzigd.
    
    Verstreken: 00:00:00.01
    MHO%xe> with t as ( -- generating your data here, simplified by me due to cat and lawn
      2  select 1063 study_key
      3  ,      to_date('01-MAR-09', 'dd-mon-rr') phase_start_date
      4  ,      to_date('02-MAR-09', 'dd-mon-rr') measurement_date_taken
      5  ,      to_date('01-APR-09', 'dd-mon-rr') study_end_date
      6  from dual union all
      7  select 1063, to_date('03-MAR-09', 'dd-mon-rr') , to_date('04-MAR-09', 'dd-mon-rr') , to_date('01-APR-09', 'dd-mon-rr') from dual union all
      8  select 1063, to_date('03-MAR-09', 'dd-mon-rr') , to_date('09-MAR-09', 'dd-mon-rr') , to_date('01-APR-09', 'dd-mon-rr') from dual union all
      9  select 1063, to_date('03-MAR-09', 'dd-mon-rr') , to_date('14-MAR-09', 'dd-mon-rr') , to_date('01-APR-09', 'dd-mon-rr') from dual union all
     10  select 1063, to_date('03-MAR-09', 'dd-mon-rr') , to_date('19-MAR-09', 'dd-mon-rr') , to_date('01-APR-09', 'dd-mon-rr') from dual union all
     11  select 1063, to_date('22-MAR-09', 'dd-mon-rr') , to_date('23-MAR-09', 'dd-mon-rr') , to_date('01-APR-09', 'dd-mon-rr') from dual union all
     12  select 1063, to_date('22-MAR-09', 'dd-mon-rr') , to_date('30-MAR-09', 'dd-mon-rr') , to_date('01-APR-09', 'dd-mon-rr') from dual
     13  ) -- actual query:
     14  select study_key
     15  ,      phase_start_date
     16  ,      measurement_date_taken
     17  ,      study_end_date
     18  from   t
     19  union all
     20  select study_key
     21  ,      phase_start_date
     22  ,      measurement_date_taken + level -- or rownum
     23  ,      study_end_date
     24  from ( select study_key
     25         ,      phase_start_date
     26         ,      measurement_date_taken
     27         ,      study_end_date
     28         ,      add_up
     29         from (
     30                select study_key
     31                ,      phase_start_date
     32                ,      measurement_date_taken
     33                ,      study_end_date
     34                ,      study_end_date - max(measurement_date_taken) over (partition by study_key
     35                                                                          order by measurement_date_taken ) add_up
     36                ,      lead(measurement_date_taken) over (partition by study_key
     37                                                          order by measurement_date_taken ) last_rec
     38                from   t
     39              )
     40         where last_rec is null
     41       )
     42  where rownum <= add_up
     43  connect by level <= add_up;
    
     STUDY_KEY PHASE_START_DATE    MEASUREMENT_DATE_TA STUDY_END_DATE
    ---------- ------------------- ------------------- -------------------
          1063 01-03-2009 00:00:00 02-03-2009 00:00:00 01-04-2009 00:00:00
          1063 03-03-2009 00:00:00 04-03-2009 00:00:00 01-04-2009 00:00:00
          1063 03-03-2009 00:00:00 09-03-2009 00:00:00 01-04-2009 00:00:00
          1063 03-03-2009 00:00:00 14-03-2009 00:00:00 01-04-2009 00:00:00
          1063 03-03-2009 00:00:00 19-03-2009 00:00:00 01-04-2009 00:00:00
          1063 22-03-2009 00:00:00 23-03-2009 00:00:00 01-04-2009 00:00:00
          1063 22-03-2009 00:00:00 30-03-2009 00:00:00 01-04-2009 00:00:00
          1063 22-03-2009 00:00:00 31-03-2009 00:00:00 01-04-2009 00:00:00
          1063 22-03-2009 00:00:00 01-04-2009 00:00:00 01-04-2009 00:00:00
    
    9 rijen zijn geselecteerd.
    

    Is there a simpler way (in SQL), I hope that others join, and share their ideas/example/thoughts.
    I feel that it is using more resources there.
    But I have to cut the daisies before now, they interfere my 'grass-green-ess";)

  • How can I do a new insert data in the other table extrancting?

    Hello, I'm looking how to do an insertion of two tables save values and apply to other tables like this:

    The insert I'm looking for is something like this:

    Have two tables, the first table is emp1 and contains the column: data 1 and 2 with line of data1: 1234 and line data 2:5555, therefore,.

    I want to create an insert on a new table called (emp2) that contain the data in the column: data 1, data 2 (from the first table demo1), data 3, data 4, etc.

    How insert again I can put / call data from another table insert in this table (emp2).

    I don't know if my example is correct (my English is very bad).

    This is an example: (sorry for the points).

    table EMP2:

    1 data... 2 data... 3 data... data 4... data 5
    (from emp1)... (from emp1)... 8534... 2347... 4544

    Thanks for the help.

    Published by: user10182692 on 24-May-2009 08:39

    Published by: user10182692 on 24-May-2009 08:40

    Published by: user10182692 on 24-May-2009 08:45

    Welcome! on the forum.

    You can include the code and data in the {code} tags

    If I'm good, and I know what is your condition

    insert into emp2 ( data1, data2)
    select data1, data2
    from emp1
    

    SS

  • Support for mandatory request - insert, select from the same table

    Hi all

    I need your help to write queries effectively.
    Oracle Version: 10.2.0.3.0
    OPERATING SYSTEM: UNIX

    I have a METRICS_TBL that is mentioned below table.

    CYCLE_DATE METRIC VALUE
    08/17/2008 COST-TV 100
    08/17/2008 COST-JOURNAL 50
    08/17/2008 COST-POSTALMAIL 25
    08/17/2008-PROD-TV 10
    08/17/2008-PROD-JOURNAL 25
    08/17/2008-PROD-POSTALMAIL 5

    Any data above, I have to add (Insert into select METRICS_TBL METRICS_TBL) at the same table with the records as mentioned below.

    2008-08-17 COSTPERPROD-TV 10
    08/17/2008-COSTPERPROD-LOG 2
    2008-08-17 COST PROD-POSTALMAIL 5

    Basically, I need to calculate the cost per product for each category. Depending on the settings available, metric should also be changed as COSTPERPROD and values should be cost/prod under each category.

    Can someone help me with the query.

    Thank you

    Something like this:

    INSERT INTO metrics_tbl
    (cycle_date, metrics, value)
    SELECT cost.cycle_date
             , 'COSTPERPROD-'||cost.mtype
             ,cost.value / prod.value
    FROM  (
       select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric
       where substr(metrics, 1, 4) = 'PROD'
       ) prod
    INNER JOIN (   select cycle_date,substr(metrics, 1, 4) mtype, substrmetrics,instr(metrics,'-')+1) mmetric
       where substr(metrics, 1, 4) = 'COST'
       ) cost on cost.cycle_date = prod.cycle_date and cost.mmetric = prod.mmetric
    

    Ideally you would divide your METRICS column into two: one for the thing you're measuring (e.g. TV or NEWSPAPER) and the other for the metric (COST, PROD, COSTPERPROD etc.). That's what I did in the views online. Without it, it's kinda a mess and behave so, if the table becomes much more.

    HTH

    Nigel cordially

    Edited by: nthomas on January 14, 2009 15:57 - small correction SQL

Maybe you are looking for