Update a table using COUNT (*)

I was wondering if there is a way to write the following code uses a single stmt:
FOR update_rec IN ( SELECT CODE, COUNT(APPOINTMENT) TOT FROM TEST GROUP BY CODE )
LOOP
 UPDATE TEST
 SET TOT_APPOINTMENT = update_rec.TOT
  WHERE CODE = update_rec.CODE;
END LOOP;
Kind regards.

You can use what is called a statement update correlated . Note that this is not tested:

UPDATE test t1
SET    tot_appointment = ( SELECT COUNT(*)
                           FROM   test t2
                           WHERE  t1.code = t2.code
                         )
;

Tags: Database

Similar Questions

  • Update a table using the clause

    Hello

    I want to update a table using the selected values.

    Cases in the sample:


    create table as empsalary)

    Select 1 as empid, 0 in the wages of all the double union

    Select option 2, the double 0);

    Data update are as follows

    with saldata as

    (

    Select 1 as empid, 5000 as wages, 500 as double pf

    Union of all the

    Select option 2, 10000,1000 like double pf

    )

    Select empid, salary saldata

    I tried the following query but does not work

    updated set of empsalary table (empid, salary) =

    (

    Select * from)

    with saldata as

    (

    Select 1 as empid, salary, 500 5000 as pf Union double all the

    Select option 2, 10000,1000 like double pf

    )

    Select empid, salary saldata

    ) sl

    where sl.empid = empsalary.empid

    )

    I use oracle 10g.

    Help, please.

    Krishna Devi wrote:

    Hello

    I want to update a table using the selected values.

    Cases in the sample:

    create table as empsalary)

    Select 1 as empid, 0 in the wages of all the double union

    Select option 2, the double 0);

    Data update are as follows

    with saldata as

    (

    Select 1 as empid, 5000 as wages, 500 as double pf

    Union of all the

    Select option 2, 10000,1000 like double pf

    )

    Select empid, salary saldata

    I tried the following query but does not work

    updated set of empsalary table (empid, salary) =

    (

    Select * from)

    with saldata as

    (

    Select 1 as empid, salary, 500 5000 as pf Union double all the

    Select option 2, 10000,1000 like double pf

    )

    Select empid, salary saldata

    ) sl

    where sl.empid = empsalary.empid

    )

    I use oracle 10g.

    Help, please.

    Thanks for posting creates table and test data.

    The error message would have helped because it's pretty obvious that this is the problem:

    Update table empsalary

    *

    ERROR on line 1:

    ORA-00903: invalid table name

    Just remove the word "table".

  • Problem with update of table (using the subquery to retrieve value)

    Hello
    I update a table based on the value of the subquery.
    Here's the update statement.

    UPDATING temp xm
    SET xm.col1 = (SELECT DISTINCT col1
    Of
    (SELECT col1, col2 COUNT (col2)
    FROM table2
    WHERE col1 = xm.col1
    AND col2 = xm.col2
    GROUP BY col1)
    where col2 in (select... in the table3)
    )
    WHERE xm.col5 = < value >
    AND xm.col6 = < value >

    When I run this statement I get following error.
    ORA-00904: "XM". "" Col1 ": invalid identifier.

    Can someone help me why I get this error?
    Why doesn't the main table alias in the subquery?

    Is it possible to avoid this / re - write the query in a different way?

    Thank you

    Published by: user552703 on November 2, 2009 20:42

    You can nest only 1 level deep (referring to the table to be updated).

    Have you looked at using the MERGE command? It is "easier" perform updates of this nature, assuming you are using a recent version of Oracle (9 or MORE).

  • Update the table using discoverer

    Hello

    I'm trying to update a table whenever the user runs a report of the Office of the discoverer. I tried to use a function to do the job, but I get the error below.

    ORA-14551: cannot perform the DML operation within a query.

    I also tried to
    1. create a procedure and update the table in this procedure.
    2. function to create and call the procedure created earlier in this function and use it in the discoverer, but still does not work and I get the same error ORA-14551.

    Please let me know if this is possible at all the. If SO, let me know the steps to do so.

    Thank you.

    Hello

    Yes you can update the discoverer dashboards using a PL/SQL function. You must make the function a standalone transaction. for example

    FUNCTION update_record (key_field VARCHAR2) RETURN VARCHAR2
    IS
    PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
    ..
    UPDATE your_table
    ..
    COMMIT;
    ..

    Rod West

  • issue update a table using a function in pipeline

    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    CORE     10.2.0.4.0     Production
    TNS for HPUX: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    OK, I have an example simplified below what is happening
    I try to make updates or inserts in the tables and I use a function pipeline for this bed of the same tables of the to do.
    and it gives me
    ORA-04091: table DSAMSTRC.T is mutating, trigger/function may not see it
    ORA-06512: at "DSAMSTRC.GET_T_ARRAY", line 6
    My solution was to do a slider and update record row by row, but I was wondering if there is a way around this error?


    Here's my example
    DROP TABLE t;
    ------------------------------------------------------------------------------------
    
    CREATE TABLE t
    AS
           SELECT LEVEL id, 'A' txt
             FROM DUAL
       CONNECT BY LEVEL <= 10;
    
    -------------------------------------------------------------------------------------
    
    CREATE OR REPLACE TYPE t_table_type AS OBJECT
                      (id NUMBER, txt VARCHAR2 (200));
    ------------------------------------------------------------------------------------
    
    CREATE OR REPLACE TYPE t_array AS TABLE OF t_table_type;
    -----------------------------------------------------------------------------------
    
    CREATE OR REPLACE FUNCTION get_t_array (v_txt IN t.txt%TYPE)
       RETURN t_array
       PIPELINED
    IS
    BEGIN
       FOR EACH IN (SELECT id, txt
                      FROM t
                     WHERE t.txt = v_txt)
       LOOP
          PIPE ROW (t_table_type (EACH.id, EACH.txt));
       END LOOP;
    
       RETURN;
    END;
    ---------------------------------------------------------------------------------------
    
    UPDATE t
       SET txt = 'B'
     WHERE id IN (SELECT id FROM TABLE (get_t_array ('A')));
     

    You can create a global temporary table that has the same columns that use your function to insert in there, and then use it to update t.

    CREATE GLOBAL TEMPORARY TABLE t_temp
    (id    INTEGER,
     txt  VARCHAR2(100))
    ON COMMIT DELETE ROWS;
    
    INSERT INTO t_temp
      (SELECT id, txt FROM get_t_array('A'));
    
    UPDATE t
       SET t.txt = (SELECT txt FROM t_temp
                     WHERE t_temp.id = t.id)
     WHERE t.id IN (SELECT id FROM t_temp);
    
  • update a table using a date to_char

    Hello all;

    I have the following query and the table below
    create table t1
    ( 
           cid varchar2(200),
           yearid varchar2(200),
           begin_date date
           );
    
     insert into t1
               (cid, yearid, begin_date)
             values
               ('A', '2010_1', sysdate);
      insert into t1
               (cid, yearid, begin_date)
             values
               ('B', '2010_1', to_date('03/04/2011', 'MM/DD/YYYY'));
    insert into t1
               (cid, yearid, begin_date)
             values
               ('C', '2010_1', to_date('01/02/2011', 'MM/DD/YYYY'));
    and I have the following update statement
       update t1 t
           set t.yearid = '2011_1'
           where to_char(t.begin_date, 'YYYY') = substr(t.yearid, 1, 4);
    what I'm trying is basically update table t1 yearid which is a varchar2 in to_char start the date are irregular... so in my case of the sample,

    TO_CHAR date start back a 2011
    Therefore, the yearid must be 2011_1 instead.

    However, my udate statement does not work well

    Don't know what exactly you want, but it looks like everything that you need is to change = to! = where year clause and using begin_date in update the value:

    SQL> select * from t1;
    
    CID YEARID     BEGIN_DAT
    --- ---------- ---------
    A   2010_1     26-JAN-11
    B   2010_1     04-MAR-11
    C   2010_1     02-JAN-11
    
    SQL> update t1 t
      2         set t.yearid = to_char(t.begin_date, 'YYYY') || '_1'
      3         where to_char(t.begin_date, 'YYYY') != substr(t.yearid, 1, 4);
    
    3 rows updated.
    
    SQL> select * from t1
      2  /
    
    CID YEARID     BEGIN_DAT
    --- ---------- ---------
    A   2011_1     26-JAN-11
    B   2011_1     04-MAR-11
    C   2011_1     02-JAN-11
    
    SQL> 
    

    SY.

  • Update the Table with the push button

    Hi I want to update my table using the push button that requires a transfer of account

    For example a single account transactions into 2nd account


    I used this query, but his does not work

    Update of cb
    Set cb_acc_id =: block3.acc_id_target
    where
    cb_acc_id =: block3.acc_id_source;



    Concerning

    Wasim Ismail

    You may forgot to validate your update...

    BTW:
    your form will not recognize, you start a transaction and make an update in the database.

  • What API can be used to update the table cs_estimate_details (repair)

    I need to update the columns 'pricing_context' and 'pricing_attribute1' in the cs_estimate_details table.

    Which API can be used to update the columns. Where can I update the table directly?

    Try to use this "CS_Charge_Details_PVT" which in turn call "CS_ESTIMATE_DETAILS_PKG".

  • Where can I download use-counter-300 - update.zip?

    Notes for version found at https://my.VMware.com/group/VMware/get-download?downloadGroup=UMSV3 , we should be able to update the counter use 2.3.2 3.0 using a file called use-counter-300 - update.zip, but there is no URL provided, only the complete OVA file download. The file will appear on the site any time soon?

    I just checked the update zip appears now.  Please confirm that you are able to download.

    Please see the release notes especially the part about removing the password by e-mail before moving because of the new encryption mechanism.

  • Update of the data in the table using LAG/LEAD

    Hello!

    I have a table that looks like:

    CREATE TABLE CUSTOMER_INFO_TEST
    (
    ACCOUNT_NUM VARCHAR2 (40 BYTE),
    PHONE VARCHAR2 (100 BYTE),
    E-MAIL VARCHAR2 (300 BYTE),
    DATE OF START_DT,
    DATE OF CHANGE_DT,
    END_DT DATE
    );

    The example data:

    INSERT INTO CUSTOMER_INFO_TEST VALUES ('BOB', 555-1234', ", TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-06', 'YYYY-MM-DD'), TO_DATE ('2011-01-10', 'YYYY-MM-DD'));
    INSERT INTO CUSTOMER_INFO_TEST VALUES ('BOB', 555-1234', ' BOB@GMAIL.) COM', TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-11', 'YYYY-MM-DD'), NULL);
    INSERT INTO CUSTOMER_INFO_TEST VALUES ('BOB', 555-1234', ' BOB@GMAIL.) COM', TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-15', 'YYYY-MM-DD'), NULL);
    INSERT INTO CUSTOMER_INFO_TEST VALUES ('JACK', 555-4321', ", TO_DATE ('01-03-2011', 'YYYY-MM-DD'), TO_DATE ('2011-03-06', 'DD-MM-YYYY'), NULL);
    INSERT INTO CUSTOMER_INFO_TEST VALUES ('JACK', 555-4321', ' JACK@GMAIL.) COM', TO_DATE ('01-03-2011', 'YYYY-MM-DD'), TO_DATE ('2011-03-11', 'YYYY-MM-DD'), NULL);

    My question:
    How can I configure end_dt (if null), to the next change_dt minus one

    It shows what I want to do:

    Select the rowid, account_num, phone, e-mail, start_dt, change_dt, end_dt, nvl (end_dt, lead (change_dt-1, 1) over (partition by account_num of start_dt order)) enddt CUSTOMER_INFO_TEST where end_dt is null;

    So, I want to update the table itself with the date in enddt. But how do I do this?

    This must be done in a single statement...

    Thanks in advance

    Richard

    Published by: user6702107 on 05-Jan-2011 09:11

    Edited by: Rydman on April 17, 2012 15:01

    Please post sample data!
    If your query returns the desired results, you can use the MERGE:

    SQL> select *
      2  from   customer_info_test;
    
    ACCOUNT_NU PHONE      EMAIL                     START_DT CHANGE_D END_DT
    ---------- ---------- ------------------------- -------- -------- --------
    BOB        555-1234                             01-01-11 06-01-11 10-01-11
    BOB        555-1234   [email protected]             01-01-11 11-01-11
    BOB        555-1234   [email protected]             01-01-11 15-01-11
    JACK       555-4321                             01-03-11 06-03-11
    JACK       555-4321   [email protected]            01-03-11 11-03-11
    
    5 rows selected.
    
    SQL> --
    SQL> merge into customer_info_test a
      2  using ( select rowid rid
      3          ,      nvl(end_dt, lead(change_dt-1, 1) over (partition by account_num order by start_dt)) new_end_dt
      4          from   customer_info_test
      5          where  end_dt is null
      6        ) b
      7  on (a.rowid = b.rid )
      8  when matched then update set a.end_dt = b.new_end_dt;
    
    4 rows merged.
    
    SQL> --
    SQL> select *
      2  from   customer_info_test;
    
    ACCOUNT_NU PHONE      EMAIL                     START_DT CHANGE_D END_DT
    ---------- ---------- ------------------------- -------- -------- --------
    BOB        555-1234                             01-01-11 06-01-11 10-01-11
    BOB        555-1234   [email protected]             01-01-11 11-01-11 14-01-11
    BOB        555-1234   [email protected]             01-01-11 15-01-11
    JACK       555-4321                             01-03-11 06-03-11 10-03-11
    JACK       555-4321   [email protected]            01-03-11 11-03-11
    
    5 rows selected.
    
  • How to count the number of columns in an oracle table using sql

    How to count the number of columns in an oracle table using sql

    You must put the name of the table in capital letters

    As

    SELECT COUNT(1)
      FROM user_tab_columns
    WHERE table_name = 'EMP';
    
    or
    
    SELECT COUNT(1)
      FROM user_tab_columns
    WHERE table_name = UPPER('Emp');
    

    Concerning
    Arun

  • Normalize the names in a huge table using UTL_MATCH

    Hello

    I have a large table (350 million records) with a "full name" column

    This column has a few typos, so I have to 'normalise' the data (only for this column), using UTL_MATCH. JARO_WINKLER_SIMILARITY.

    I did some tests with a small table, and it works to show the similar names:

    SELECT b.SID, b.name FROM typotable a, typotable b utl_match.jaro_winkler_similarity (b.SID, b.name) WHERE BETWEEN 85 and 99 AND a.rowid > b.rowid;


    But:


    (1) the test table was small, by using this code directly on the 350 million accounts table take ages... What can be done about it?


    (2) this shows just the similar names. How can I update the table by searching for similarities, choose one of them as the only value for each name?




    Thank you

    1590733 wrote:

    Yes, I get your point. The thing is that there is no "correct" available names and the original table is huge, that's what I thought:

    -Create a table of secondary NAMES, with unique names. These names would have been generated by match the values similar to one of them (but always the same, no matter if is not one that suits). This should be equivalent to your table 'correctness '.

    -Run the cleaning procedure for updating records

    How can I create this secondary NAMES table? (The column 'genre' is not serious at all, that the 'name' must be set)

    Thanks for your help

    Well, you need to determine what is the logic that would pick one of the incorrect names on the other.  In its current version, you can easily get two incorrect values having the same value of match.  But then you must also consider what creates a 'group' of values that you can get the best in the group.  Using the match itself is not enough to create groups.

    Example:

    SQL > ed

    A written file afiedt.buf

    1 Select a.fname as $fname1, b.fname as fname2

    2, utl_match.jaro_winkler_similarity (a.fname, b.fname) as a match

    3 typotable one

    4 join typotable b on (a.fname! = b.fname)

    where the 5 utl_match.jaro_winkler_similarity (a.fname, b.fname) > = 85

    6 * 1.3 desc order

    SQL > /.

    $FNAME1 FNAME2 MATCH

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

    FROCEN FROZEN 92

    FROZEN FROCEN 92

    FROZEN FROCEN 92

    FROZEN FROZEN 92

    JELLY FROZIN 93

    JELLY FROCEN 92

    FROZEN FROZEN 92

    FROZEN FROZIN 93

    WHIPLASH WIPLASH 96

    WHIPLASH WIPLASH 96

    10 selected lines.

    As you can see, for example, FROCEN has two possible variants, both with a football match of the 92.  The same with others.

    However, you could start cutting things around (and it's really a hack) to get something like:

    SQL > ed

    A written file afiedt.buf

    1 with t as)

    2. Select a.fname as $fname1, b.fname as fname2

    3, utl_match.jaro_winkler_similarity (a.fname, b.fname) as a match

    typotable a 4

    5 join typotable b on (a.fname! = b.fname)

    where the 6 utl_match.jaro_winkler_similarity (a.fname, b.fname) > = 85

    7       )

    8, ch. as)

    9 select $fname1, ($fname1, fname2) greatest as fname2, match

    10, (select count (*)

    11 t t2

    12 where t2.fname2 = t.fname2

    13 and t2.fname1! = t.fname1

    (14) as the NTC

    15 t

    16       )

    17, r as)

    18 select $fname1, fname2, match, cnt

    19, row_number() over (partition by $fname1 by cnt desc, desc match order): the nurse

    20 c

    21       )

    22 select $fname1, fname2

    23 r

    where the 24 rn = 1

    25 * order by 1

    SQL > /.

    $FNAME1 FNAME2

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

    FROZEN FROCEN

    FROZEN FROZEN

    FROZEN FROZEN

    FROZIN FROZIN

    WHIPLASH WIPLASH

    WIPLASH WIPLASH

    6 selected lines.

    but then it depends on your data as to if it will work in all circumstances

  • Update of table with 14 million records

    good day, I'm updating a table with 14 million documents column. using the data from another table.
    This takes terrible time just updated to 500,000 over the weekend, which is unacceptable.
    ideas to improve my code and get the fastest update is much appreciated.

    DECLARE
    TYPE vc_type is table of index varchar2 (20) by pls_integer;
    type tabtype_rowid is table of pls_integer rowid indexes;
    my_cur sys_refcursor;
    cur_limit integer: = 10000;

    tab_rowid tabtype_rowid;

    Start
    OPEN FOR My_cur
    SELECT value, rowid
    TABLE 1A

    where the value IS NOT NULL
    and id is null;


    loop
    collect the fetch my_cur in bulk in tab_value, cur_limit limit tab_rowid;

    FORALL k IN 1.tab_npi.count

    UPDATE table 1A
    A.ID = (select id SET
    in Table 2b
    where value = tab_value (k)
    and rownum = 1
    )
    WHERE a.rowid = tab_rowid (k);

    COMMIT;

    When the output tab_value.count < cur_limit;.
    end loop;

    END;


    Thank you

    903537 wrote:
    I tried to update basic training, as a first step and it crashed...

    He didn't fail, it failed.

  • Programatically update af:table when clicking on createInsert af:button

    Hi all

    I use JDeveloper 11.1.2.4.0.

    Requirement:

    I have an af:panelCollection, inside I have a (Add) af:button and an af:table. Af: button creates an empty line in a table as createInsert. Now when I click on the Add button, it creates the empty line in the af: table. But the blank row is not visible immediately. It is visible only when I do another action in this page, as on some other tab and back.

    From my observation, I found that the updating of the table does not occur immediately.

    I want to create a blank line like createInsert done and immediately refresh the table so that the empty line is visible in the table.

    I tried to adjust the partialTrigger of the tab; the reference to the id of the af: button, but still not working.

    I tried to update the table in a pragmatic way during the click on the button, but did not work.

    Can anyone suggest me solution for this.

    Thank you and best regards,

    Susanto

    Try to update af:panelCollection and see.

  • Get the 500 error trying to create a table using the REST API

    Hello

    I tried to create a table using the REST API for Business Intelligence Cloud, but I got 500 Internal Server Error for a while now.

    Here are the details that I use to create a table.

    Capture.JPG

    and the json to create the schema that I use is

    [{'Nullable': [false], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [18], 'columnName': ["ROWID"]}]

    , {'Nullable': [true], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [18], 'columnName': ['RELATIONID']},

    {'Nullable': [true], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [18], 'columnName': ['ID']}

    , {'Nullable': [true], 'defaultValue': 'dataType' [null],: ['TIMESTAMP'], 'precision': [0], 'length': [0], 'columnName': ['RESPONDEDDATE']},

    {'Nullable': [true], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [255], 'columnName': ['RESPONSE']},

    {'Nullable': [false], 'defaultValue': 'dataType' [null],: ['TIMESTAMP'], 'precision': [0], 'length': [0], 'columnName': ['SYS_CREATEDDATE']},

    {'Nullable': [false], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [18], 'columnName': ['SYS_CREATEDBYID']},

    {'Nullable': [false], 'defaultValue': 'dataType' [null],: ['TIMESTAMP'], 'precision': [0], 'length': [0], 'columnName': ['SYS_LASTMODIFIEDDATE']},

    {'Nullable': [false], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [18], 'columnName': ['SYS_LASTMODIFIEDBYID']},

    {'Nullable': [false], 'defaultValue': 'dataType' [null],: ['TIMESTAMP'], 'precision': [0], 'length': [0], 'columnName': ['SYS_SYSTEMMODSTAMP']},

    {'Nullable': [false], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [10], 'columnName': ['SYS_ISDELETED']},

    [{'Nullable': [true], 'defaultValue': 'dataType' [null],: ['VARCHAR'], 'precision': [0], 'length': [50], 'columnName': ['TYPE']}]

    I tried this using postman and code, but I always get the following response error:

    Error 500 - Internal server error

    Of RFC 2068 Hypertext Transfer Protocol - HTTP/1.1:

    10.5.1 500 internal Server Error

    The server encountered an unexpected condition which prevented him from meeting the demand.

    I am able to 'get' existing table schemas, delete the tables, but I'm not able to make put them and post operations. Can someone help me to identify the problem, if there is no fault in my approach.

    Thank you

    Romaric

    I managed to create a table successfully using the API - the only thing I see in your JSON which is different from mine is that you have square brackets around your values JSON where I have not. Here is my CURL request and extract my JSON file (named createtable.txt in the same directory as my CURL executable):

    curl u [email protected]: password UPDATED h x ' X-ID-TENANT-NAME: tenantname ' h ' Content-Type: application/json '-binary data @createtable.txt https://businessintell-tenantname.analytics.us2.oraclecloud.com/dataload/v1/tables/TABLE_TO_CREATE k

    [

    {

    'columnName': 'ID',

    'dataType': 'DECIMAL ',.

    'Length': 20,.

    "accuracy": 0.

    'Nullable': false

    },

    {

    'columnName': 'NAME',

    'dataType': 'VARCHAR ',.

    'Length': 20,.

    "accuracy": 0.

    'Nullable': true

    },

    {

    "columnName': 'STATUS."

    'dataType': 'VARCHAR ',.

    'Length': 20,.

    "accuracy": 0.

    'Nullable': true

    },

    {

    "columnName': 'CREATED_DATE."

    'dataType': 'TIMESTAMP '.

    'Length': 20,.

    "accuracy": 0.

    'Nullable': true

    },

    {

    'columnName': 'UPDATED_DATE ',.

    'dataType': 'TIMESTAMP '.

    'Length': 20,.

    "accuracy": 0.

    'Nullable': true

    }

    ]

Maybe you are looking for