当前位置:网站首页>mysql/oracle 以唯一时间为分界,小于等于该时间求和,大于该时间求和
mysql/oracle 以唯一时间为分界,小于等于该时间求和,大于该时间求和
2022-06-12 03:17:00 【紫薯馍馍】
【背景】 项目需要把上百个乡镇的用户数进行分组统计,本次分类以每个乡镇大批量用户割接时间为分界线,统计上线前和上线后的用户发展情况,如下图,就需要统计 小于等4月18日的数据和之后日期的所有数据,以前没遇到过类似的问题,在这里做个记录。

【思路】本来想在网上抄作业,但是看到的基本上都是类似于按照时间区间统计的需求,说下自己的思路,因为项目里不同乡镇的割接时间是不一致的,所以首先要确定分界线,也就是找到不同乡镇最大割接的日期,这里我先创建了一张表用来做分界线使用
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随后,以其中一个乡镇为例,找到它小于等于割接时间的数据,然后分组求和,这里就是我要统计的底量数据
-- 底量部分
select t.areaname,sum(t.pv) from test_SZXC t,(select * from test_szxc_1 where areaname like '%关坝%')a
where
t.areaname = a.areaname
and
t.ctime <= a.ctime
group by areaname
同理,大于该时间的就是增量数据了,这里我就直接全表处理了,
-- 增量部分
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
最后发现此法可行,就把两部分数据放在一起处理了,加了一个列作为区分使用
-- 数据合并
select '底量' 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 '增量' 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最后导出即可
边栏推荐
- Calculus review 2
- [digital signal processing] correlation function (energy signal | cross correlation function of energy signal | autocorrelation function of energy signal)
- Application of ankery anti shake electric products in a chemical project in Hebei
- Laravel 8 selects JWT for interface verification
- 大整数的加与乘;
- Simple database connection example
- Computer configuration suggestions for learning modeling
- 无限循环判断方法;
- xml
- Wechat applet project example - Fitness calculator
猜你喜欢

errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306; Postman error

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?

Redis gets the set of keys prefixed with XXX

Kubernetes affinity learning notes

How to prevent electrical fire in shopping malls?

Lighting Basics: optical model

Inverted string - two solutions

如何防止商场电气火灾的发生?
![[Business Research Report] analysis report on online attention of China's e-sports industry in 2021 - download link attached](/img/f3/87fd24e4944c83475f49ec5e56c640.jpg)
[Business Research Report] analysis report on online attention of China's e-sports industry in 2021 - download link attached

分數大小的比較
随机推荐
微信小程序项目实例——我有一支画笔(画画)
微积分复习2
Restful interface design specification [for reference only]
How to build urban smart bus travel? Quick code to answer
Introduce the functions of the new project aleo
Special materials | household appliances, white electricity, kitchen electricity
Demand and business model innovation - demand 8- interview
利用ssh公钥传输文件
errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
oracle之序列
Wechat applet project example - renju for two
In 2022, don't you know the difference between arrow function and ordinary function?
Quelles sont les solutions dans tous les domaines?
Requirements and business model innovation - Requirements 12 - process oriented modeling
MySQL partition table create delete modify view
cupp字典生成工具(同类工具还有crunch)
Redis gets the set of keys prefixed with XXX
Calculus review 2
The four pain points of enterprise digitalization are solved by low code platform
Apache simple honeypot