当前位置:网站首页>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最后导出即可
边栏推荐
- 如何防止商場電氣火灾的發生?
- [Hongmeng] use the timer to play a simple game of robbing red envelopes
- Final summary of addition, deletion, modification and query - 2.1 (single table - addition, deletion, modification and query)
- Infinite loop judgment method;
- 微信小程序项目实例——双人五子棋
- Demand and business model innovation - demand 10- observation and document review
- ssh公钥登录失败报错:sign_and_send_pubkey: no mutual signature supported
- 简单的数据库连接示例
- Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
- 2020-12-07
猜你喜欢

【点云压缩】Sparse Tensor-based Point Cloud Attribute Compression

laravel 8 选用 jwt 进行接口验证

Drawcall, batches, setpasscall in unity3d

Demand and business model innovation - demand 6- stakeholder analysis and hard sampling

Machine learning - dimensionality reduction (data compression, data visualization)

字符串处理:

Exemple de projet d'applet Wechat - calculatrice de constitution

2020-12-06

$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError

In 2022, don't you know the difference between arrow function and ordinary function?
随机推荐
Infinite loop judgment method;
Demand and business model innovation - demand 6- stakeholder analysis and hard sampling
跨域有哪些解决方法?
Selection (046) - what is the output of the following code?
Machine learning - dimensionality reduction (data compression, data visualization)
2020-12-17
Wechat applet project example - I have a paintbrush (painting)
Key points of code neatness (III)
What is the commonly heard sub table of MySQL? Why does MySQL need tables?
微信小程序項目實例——體質計算器
SSH public key login failed with error: Sign_ and_ send_ pubkey: no mutual signature supported
errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
Drawcall, batches, setpasscall in unity3d
Sparse tensor based point cloud attribute compression
2020-12-17
oracle之模式对象
Oracle schema object
I2C protocol overview
2020-12-07
How to prevent electrical fire in shopping malls?