当前位置:网站首页>Mysql/oracle takes the unique time as the boundary, and the sum is less than or equal to this time, and greater than this time

Mysql/oracle takes the unique time as the boundary, and the sum is less than or equal to this time, and greater than this time

2022-06-12 03:19:00 Purple potato bun

【 background 】 The project needs to group the number of users in hundreds of towns , This classification takes the cut-off time of a large number of users in each township as the dividing line , Statistics of user development before and after the launch , Here's the picture , You need statistics Less than equal 4 month 18 Data of the day and all data of subsequent days , I haven't encountered any similar problems before , Make a record here .

【 Ideas 】 I wanted to copy my homework on the Internet , But what we see is basically similar to the demand statistics based on time interval , Say your own thoughts , Because the cut-off time of different towns in the project is inconsistent , So first of all, we must determine the dividing line , That is, the date when the largest cut-off was found in different towns , Here, I first created a table to use as a dividing line

create table test_szxc_1 as 
SELECT T.areaname,T.ctime,PV FROM test_SZXC T,
(SELECT T.areaname,MAX(PV) pp FROM test_SZXC T 
 group by T.areaname )a
where  a.areaname = t.areaname
and  a.pp = t.PV

And then , Take one of the towns as an example , Find the data that is less than or equal to the cutover time , Then sum them in groups , Here is the bottom data I want to count


--  The bottom part 
select t.areaname,sum(t.pv) from test_SZXC t,(select * from test_szxc_1 where areaname like '% Close the dam %')a
where 
t.areaname = a.areaname
and 
t.ctime <= a.ctime
group by areaname

Empathy , What is greater than this time is incremental data , Here I will directly handle the whole table ,

--  The incremental part 
select t.areaname,sum(t.pv) from test_SZXC t,(select * from test_szxc_1 )a
where 
t.areaname = a.areaname
and 
t.ctime > a.ctime
group by areaname

Finally, it is found that this method is feasible , Just put the two parts of data together for processing , A column is added to distinguish



--  Data merging 

select ' Bottom volume ' as category ,t.areaname,sum(t.pv) from test_SZXC t,(select * from test_szxc_1)a
where 
t.areaname = a.areaname
and 
t.ctime <= a.ctime
group by areaname
 union all
select ' The incremental ' as category,t.areaname,sum(t.pv) from test_SZXC t,(select * from test_szxc_1 )a
where 
t.areaname = a.areaname
and 
t.ctime > a.ctime
group by areaname

Finally, export

原网站

版权声明
本文为[Purple potato bun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120316522863.html