当前位置:网站首页>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最后导出即可
边栏推荐
- Domestic mobile phones are snubbing low-end consumers, and Nokia provides them with high-quality products
- Special information | liquor (Baijiu, beer, wine)
- Go syntax variable
- 2020-12-06
- Recommend 6 office software, easy to use and free, double the efficiency
- [string] judge whether S2 is the rotation string of S1
- Selection (044) - what is the output of the following code?
- What is the commonly heard sub table of MySQL? Why does MySQL need tables?
- Unity3D中DrawCall、Batches、SetPassCall
- Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model
猜你喜欢
![[Hongmeng] use the timer to play a simple game of robbing red envelopes](/img/27/32b65dc90db7f6ece24ad39ff9b0ef.png)
[Hongmeng] use the timer to play a simple game of robbing red envelopes

Wechat applet project example - renju for two

2020-12-07

string manipulation:

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

Cupp dictionary generation tool (similar tools include crunch)

Comment prévenir les incendies électriques dans les centres commerciaux?

Application of ard3m motor protector in coal industry

Demand and business model innovation - demand 9- prototype

errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
随机推荐
Interpreting Julia's 2021: step by step towards the mainstream programming language
I2C协议概述
[Business Research Report] 2021 global mobile game player white paper - download link attached
Kubernetes affinity learning notes
Restful interface design specification [for reference only]
Demand and business model innovation - demand 11 - overview of demand analysis
【点云压缩】Variational Image Compression with A Scale Hyperprior
$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
Selection (046) - what is the output of the following code?
errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306;postman报错
MySQL partition table create delete modify view
2020-12-17
Oracle schema object
无限循环判断方法;
Application of acrelcloud-6000 secure power cloud platform in a commercial plaza
2020-12-17
微积分复习2
How to prevent electrical fire in shopping malls?
Application of ard3m motor protector in coal industry
分數大小的比較