Table A: Table B data modification

Source: Internet
Author: User

Table A modifies table B's data function. Today, the calculation data error is caused by the problem of the original project method. Now, you need to change the previous data. The table to be changed is as follows: table A (trainee training info table) [SQL] stus_id INTEGER not null, stu_idcard VARCHAR2 (50) not null, mas_idcard VARCHAR2 (30), stus_starttime DATE not null, stus_endtime DATE, stus_totalminute INTEGER, stus_totalmileage INTEGER, stus_sinfoid INTEGER not null, stus_checktype VARCHAR2 (50), stus_isok VARCHAR2 (30) default 1, stus_carid INTEGER, stus_deviceid VARCHAR 2 (50) not null, ds_id INTEGER, consumetypeid INTEGER default 0 not null, createdate DATE, sumofconsumption NUMBER (18, 2), createby INTEGER, stus_isnormal INTEGER default 1, stus_errorcausa VARCHAR2 (100 ), region_ratio NUMBER default 0 not null, region_min_ratio NUMBER default 0 not null, options VARCHAR2 (200), stus_ispass INTEGER default 1, stus_remark VARCHAR2 (1000), regionids VARCHAR2 (1000) Table B (student Time Table) [SQL] stut_id INTEGER not null, stu_idcard VARCHAR2 (30) not null, stut_sinfoid INTEGER not null, stut_totalperiod INTEGER not null, stut_lasttime DATE not null, stut_totalmileage INTEGER not null, error_totalperiod INTEGER default 0 not null, error_totalmileage INTEGER default 0 not null, stut_nums INTEGER default 0 not null according to the ID card (stu_idcard), project number (stus_sinfoid) in Table) to accumulate stus_totalmileage. Stut_totalmileage in the column value corresponding to table B First queries the data of Table A to be modified: [SQL] select. stu_idcard,. stus_sinfoid, sum (. stus_totalmileage) from a group by. stu_idcard,. stus_sinfoid order by stu_idcard according to the data obtained above to change the stut_totalmileage of Table B. At first, some methods were found in Baidu, but no actual conversion was possible, finally, we can see that the merge into function can only be used in 9i and later versions. Note: When writing SQL statements, we often encounter a large number of Insert/Update statements at the same time, that is, when there is a record, Update (Update), when there is no data, insert ). Syntax: [SQL] MERGE INTO table_name alias1 USING (table | view | sub_query) alias2 ON (join condition) WHEN MATCHED THEN UPDATE table_name SET col1 = col_val1, col2 = col2_val when not matched then insert (column_list) VALUES (column_values) [WHERE col1. ..]; according to the preceding syntax logic, the following SQL statement can be obtained: [SQL] merge into t_ds_stutime me using (select * from (select ec. stu_idcard, ec. stus_sinfoid, sum (ec. stus_totalmileage) cc from t _ Ds_stagerec ec group by ec. stu_idcard, ec. stus_sinfoid order by ec. stu_idcard) kk on (me. stu_idcard = kk. stu_idcard and me. stut_sinfoid = kk. stus_sinfoid) when matched then update set me. stut_totalmileage = kk. cc where me. stut_totalmileage! = Kk. cc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.