Basic knowledge of SQL in Oracle mode SQL code -- create table and data for testing create table t603 (code varchar2 (10), year varchar2 (10), month varchar2 (10 ), p_id varchar2 (7), v1 number (10), v2 number (10); comment on column t603.code is 'factory '; comment on column t603.year is 'Year '; comment on column t603.month is 'month'; comment on column t603.p _ id is 'product'; comment on column t603.v1 is 'product'; comment on column t603.v2 is 'sale '; insert into t603 Values ('2017030', 600001, '30', 600001); insert into t603 values ('20170101', '31 ); insert into t603 values ('2017010', 600001, '10', 600002); insert into t603 values ('2017010', '10 ); insert into t603 values ('20170101', 600002, '31', 600002); insert into t603 values ('20170101', '30 ); select * from t603; -- aggregate SQL code select code, p_id, v1 from t603 where code in ('20160301', '20160301') according to custom rules ') Model return updated rows -- the return updated rows clause after MODEL limits the results to those rows created or updated in the query so that the result set contains only the new calculated values. Partition by (code) -- partition dimension by (p_id) -- dimension measures (v1) -- measure rules (v1 ['err1'] = v1 ['30'] + v1 ['31']-v1 ['10']) -- calculate the rule order by code, p_id; Output codep_idv11600001err10268242err11 -- aggregate by multiple rules, return the SQL code select code, year, p_id, month, including non-aggregate rows, v1 from t603 where year = '000000' model partition by (code, year) dimension by (p_id, month) measures (v1, v2) rules (v1 ['err1 ', 11] = v1 ['30', 11] + v1 ['31', 11]-v1 ['10', 11], v2 ['err1 ', 12] = v2 ['30', 12] + v2 ['31', 12]-v2 ['10', 12]) order by code, p_id; output dimension -- specify the dimension value to be aggregated SQL code select code, p_id, month, v1 from t603 where year = '000000' model return updated rows partition by (code) dimension by (p_id, month) measures (v1) rules (v1 ['err1', for month in (11,12)] = v1 ['30', cv (month)] + v1 ['31', cv (month)]-v1 ['10', cv (month)]) order by code, p_id, month; output region -- CV () the function obtains the current value of a multi-row statistical record. You can use an expression to calculate the value. Available Syntax: for month from 1 to 12 increment 1. You can also use subqueries: for year in (select year from t603)