当前位置:网站首页>hql求一个范围内最大值
hql求一个范围内最大值
2022-04-23 06:15:00 【山顶看数据】

主要明白的是
字符串是可以进行排序的,但是不能进行最极致,比如这里的日期
这道题就是求在开始时间和结束时间里面的最大的数值,而且还是把两列变成一例的方法
最终查询的答案
select user_id,
max(num) max_num
from (
select id,
user_id,
dt,
sum(p) over(partition by user_id order by dt) num
from (
select id,
user_id,
begin_date dt,
1 p
from test
union
select id,
user_id,
end_date dt,
-1 p
from test
) t1
) t2
group by user_id;
测试语句
create table if not exists test(
id int,
user_id string,
begin_date string,
end_date string
)
row format delimited
fields terminated by ',';
insert into test values(1,'A','2020-01-01','2020-01-30');
insert into test values(2,'A','2020-01-02','2020-01-30');
insert into test values(3,'A','2020-01-10','2020-02-10');
insert into test values(4,'B','2020-02-11','2020-02-30');
insert into test values(5,'C','2020-01-01','2020-01-30');
这里面我们是吧这一张表进行分解,然后再进行合并,获取我们想要的字段
-- 获取开始时间的哪一张表
select id,user_id,begin_date dt, 1 p
from test
1 A 2020-01-01 1
2 A 2020-01-02 1
3 A 2020-01-10 1
4 B 2020-02-11 1
5 C 2020-01-01 1
-- 获取结束时间的哪一张表
select id,user_id,end_date dt, -1 p
from test
1 A 2020-01-30 -1
2 A 2020-01-30 -1
3 A 2020-02-10 -1
4 B 2020-02-30 -1
5 C 2020-01-30 -1
把两张表进行合并(union)(这也是把两列变成一列)
id user_id dt p
1 A 2020-01-01 1
1 A 2020-01-30 -1
2 A 2020-01-02 1
2 A 2020-01-30 -1
3 A 2020-01-10 1
3 A 2020-02-10 -1
4 B 2020-02-11 1
4 B 2020-02-30 -1
5 C 2020-01-01 1
5 C 2020-01-30 -1
然后最这张表进行分组。然后再对dt进行排序,并对p进行求和
1 A 2020-01-01 1
2 A 2020-01-02 2
3 A 2020-01-10 3
2 A 2020-01-30 1
1 A 2020-01-30 1
3 A 2020-02-10 0
4 B 2020-02-11 1
4 B 2020-02-30 0
5 C 2020-01-01 1
5 C 2020-01-30 0
最后就是进行use_id进行分组,去最大的p即可
这个结果就是在一定范围内的最大值,也就是最大的持仓笔数
版权声明
本文为[山顶看数据]所创,转载请带上原文链接,感谢
https://blog.csdn.net/li1579026891/article/details/121764846
边栏推荐
猜你喜欢

机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装

Int8 quantification and inference of onnx model using TRT

UEFI学习01-ARM AARCH64编译、ArmPlatformPriPeiCore(SEC)

GIS实战应用案例100篇(五十二)-ArcGIS中用栅格裁剪栅格,如何保持行列数量一致并且对齐?

EMMC/SD学习小记

rearrange 和 einsum 真的优雅吗

GIS实用小技巧(三)-CASS怎么添加图例?

FATFS FAT32学习小记

基于Labview上位机的51单片机步进电机控制系统(上位机代码+下位机源码+ad原理图+51完整开发环境)

RISCV MMU 概述
随机推荐
美摄科技受邀LVSon2020大会 分享《AI合成虚拟人物的技术框架与挑战》
Wechat applet uses wxml2canvas plug-in to generate some problem records of pictures
社区版阿里MQ普通消息发送订阅Demo
安装 pycuda 出现 PEP517 的错误
PyTorch 11.正则化
PyTorch 10. 学习率
直观理解 torch.nn.Unfold
PyTorch 17. GPU concurrency
Systrace parsing
PyTorch 12. hook的用法
Swin transformer to onnx
南方投资大厦SDC智能通信巡更管理系统
大型体育赛事无线通信系统
F. The wonderful use of pad
广西电网|应急空天一体化通信系统方案
Unable to determine the device handle for GPU 0000:02:00.0: GPU is lost.
Chapter 3 pytoch neural network toolbox
1.1 pytorch and neural network
GIS实战应用案例100篇(五十三)-制作三维影像图用以作为城市空间格局分析的底图
PyTorch 9. 优化器