当前位置:网站首页>HQL find the maximum value in a range
HQL find the maximum value in a range
2022-04-23 12:57:00 【Look at the data at the top of the mountain】
The main thing to understand is
Strings can be sorted , But you can't do the best , For example, the date here
This problem is to find the largest value in the start time and end time , And it's also a way to turn two columns into one example
The answer to the final query
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;
Test statement
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');
Let's decompose this table , Then merge , Get the fields we want
-- Which table to get the start time
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
-- Which table to get the end time
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
Merge the two tables (union)( Turn... Into two columns )
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
Then group this table . And then to dt Sort , Also on p In sum
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
Finally, it's going on use_id Grouping , Go to the biggest p that will do
This result is the maximum value in a certain range , That is, the largest number of positions
版权声明
本文为[Look at the data at the top of the mountain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230615025362.html
边栏推荐
- Synchronously update the newly added and edited data to the list
- Customize the shortcut options in El date picker, and dynamically set the disabled date
- php生成json处理中文
- mysql中 innoDB执行过程分析
- Huawei cloud MVP email
- Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
- 梳理網絡IP代理的幾大用途
- Remote sensing image classification and recognition system based on convolutional neural network
- Buuctf Web [gxyctf2019] no dolls
- Stm32cubeprogrammer basic instructions
猜你喜欢
CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
4. DRF permission & access frequency & filtering & sorting
0基础可以考CPDA数据分析师证书吗
在线计算过往日期天数,计算活了多少天
Summary of JVM knowledge points - continuously updated
leetcode:437. Path sum III [DFS selected or not selected?]
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
SSM框架系列——注解开发day2-2
SSM framework series - annotation development day2-2
Kubernetes 入門教程
随机推荐
The El table horizontal scroll bar is fixed at the bottom of the visual window
Huawei cloud MVP email
How to prevent the website from being hacked and tampered with
MySQL supports IP access
STM32 control stepper motor (ULN2003 + 28byj)
SSM框架系列——注解开发day2-2
4.DRF 权限&访问频率&过滤&排序
Plato Farm-以柏拉图为目标的农场元宇宙游戏
Keyword interpretation and some APIs in RT thread
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
SSM框架系列——Junit单元测试优化day2-3
Jupiter notebook installation
Web17——EL与JSTL的使用
World Book Day: I'd like to recommend these books
Analysis of InnoDB execution process in MySQL
甲辰篇 創世紀《「內元宇宙」聯載》
Date time type in database
Sort out several uses of network IP agent
Object. The disorder of key value array after keys
NPDP|产品经理如何做到不会被程序员排斥?