简体   繁体   English

每周计算一个表中多行的总和并将其写入另一个表

[英]Calculate the SUM of multiple rows from one table and have it written to another table, weekly

I am not an experienced SQL coder and sort of learn as I go.我不是一个有经验的 SQL 编码员,并且可以边走边学。 I have a task that needs to be executed automatically on a weekly basis.我有一项需要每周自动执行的任务。 There's a few parts to this job:这项工作有几个部分:

1) Calculate the SUM of multiple rows from a couple of columns. 1)从几列计算多行的总和。

select SUM(TOTAL_COST) from TABLE_1 and select SUM(TOTAL_HOURS) from TABLE_1

2) Write the 2 SUMs from TABLE_1 to 2 columns in TABLE_2. 2) 将 TABLE_1 中的 2 个 SUM 写入 TABLE_2 中的 2 列。

Let's call these columns: SUM_OF_COST and SUM_OF_HOURS from Table_2我们将这些列称为:表_2 中的 SUM_OF_COST 和 SUM_OF_HOURS

3) This job needs to be executed weekly. 3)这项工作需要每周执行一次。

BEGIN
  DBMS_SCHEDULER.SET_ATTRIBUTE (
  name         =>  'update_job',
   attribute    =>  'repeat_interval',
   value        =>  'freq=weekly; byday=wed');
END;
/

The codes above are just from my understanding of I THINK what would be used.上面的代码只是我对我认为会使用什么的理解。 I know I have to write the entire job from the first 2 steps above into a package?我知道我必须将上述前 2 个步骤的整个工作写入一个包? I'm assuming I would reference that package in the job (step 3).我假设我会在工作中引用该包(第 3 步)。 I need help with how I would go about writing this.我需要关于如何写这篇文章的帮助。 Does the SUMs need to be written to another column before they can be written to TABLE_2 or can it calculate the SUM and then write the value to TABLE_2 without storing that value in TABLE_1? SUM 是否需要先写入另一列,然后才能写入 TABLE_2,还是可以计算 SUM 然后将值写入 TABLE_2 而不将该值存储在 TABLE_1 中? How do I piece together the code to do the calculations and then write to the second table?如何将代码拼凑起来进行计算,然后写入第二个表? Any help would be greatly, greatly, appreciated.任何帮助将不胜感激。 Thank you!谢谢!

You are in right way.你是在正确的方式。

You need to create a procedure that will be performed every week.您需要创建一个每周执行的procedure

CREATE OR REPLACE PROCEDURE sum_weekly IS
BEGIN
 INSERT INTO TABLE_2(SUM_OF_COST ,SUM_OF_HOURS ) (SELECT SUM(cost),SUM(hour) FROM TABLE_1)
 COMMIT;
END;

More about insert select syntax .更多关于插入选择语法

Then you need create schedule :然后你需要创建时间表

BEGIN
 DBMS_SCHEDULER.CREATE_JOB (
  job_name             => 'my.sum_weekly.schedule',
  job_type             => 'PLSQL_BLOCK',
  job_action           => 'BEGIN sum_weekly(); END;',
  start_date           =>  sysdate,
  repeat_interval      => 'FREQ=WEEKLY', 
  enabled              =>  TRUE,
  comments             => 'Gather table statistics from Table 1 to Table 2');
END;

Examples for repeat_interval . repeat_interval示例。

FREQ=HOURLY;INTERVAL=4
every 4 hour
FREQ=HOURLY;INTERVAL=4;BYMINUTE=10;BYSECOND=30
every 4 hours, 10 minutes, 30 seconds of;
FREQ=YEARLY;BYYEARDAY=-276
every 31 th March;
FREQ=YEARLY;BYMONTH=MAR;BYMONTHDAY=31
every 31 th March;

To check correct set interval检查正确的设置interval

DECLARE   next_run_date   TIMESTAMP;
BEGIN
 DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING (
 'FREQ=HOURLY;INTERVAL=4;BYMINUTE=10;BYSECOND=30'
 ,SYSTIMESTAMP
 ,NULL
 ,next_run_date) ;
DBMS_OUTPUT.PUT_LINE ( 'next_run_date: ' || next_run_date );
END;

To see your schedules use following query:要查看您的时间表,请使用以下查询:

SELECT job_name, state, enabled FROM user_scheduler_jobs;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和? - MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table? 由写入另一张表的行中的多列进行分区 - Partition by multiple columns written in rows from another table 对一个表中的行求和,然后将其移至另一表 - sum rows from one table and move it to another table 从具有多行相同id的表中获取每个id一行和多列的表,并根据条件计算总和 - Get a table with one row per id and multiple columns from a table with multiple rows for same id and calculate sum based on condition 从一个表计算 SUM 并显示另一个表的列 - Calculate SUM from one table and display columns from another 从一个表中选择行,其中另一表中的多行已确定值 - Select row from one table where multiple rows in another table have determined values SQL将一个表中的多行插入另一个表的列中 - SQL insert multiple rows from one table into columns on another table 在oracle中将多个行从一个表复制到另一个表 - Copy multiple rows from one table to another table in oracle 如何计算一个表中的总和,然后插入到另一个表中? - How to calculate the sum in one table and then insert into another table? MySQL 多个表中多行的总和 - MySQL SUM of multiple rows from multiple table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM