[英]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.