当前位置:网站首页>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
边栏推荐
- 只是不断地建构平台,不断地收拢流量,并不能够做好产业互联网
- bert-base-chinese下载(智取)
- 4. DRF permission & access frequency & filtering & sorting
- NPDP|产品经理如何做到不会被程序员排斥?
- Labels and paths
- 21 days learning mongodb notes
- Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
- Summary of JVM knowledge points - continuously updated
- Kubernets Getting started tutoriel
- What are the forms of attack and tampering on the home page of the website
猜你喜欢
PC starts multiple wechat at one time
31. Next arrangement
Record the problems encountered in using v-print
CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction
Process virtual address space partition
SSM框架系列——注解开发day2-2
Labels and paths
[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
梳理网络IP代理的几大用途
V-model binding value in El select, data echo only displays value, not label
随机推荐
Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
SSM framework series - annotation development day2-2
Unlock openharmony technology day! The annual event is about to open!
BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
How to click an object to play an animation
Recommended website for drawing result map
Buuctf Web [bjdctf2020] zjctf, but so
Sort out several uses of network IP agent
Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
unity常见的问题(一)
[csnote] ER diagram
如何实现点击一下物体播放一次动画
Go language array operation
Teach you to quickly develop a werewolf killing wechat applet (with source code)
Mysql8 installation
Recovering data with MySQL binlog
Free and open source charging pile Internet of things cloud platform
After the data of El table is updated, the data in the page is not updated this$ Forceupdate() has no effect
STM32 project transplantation: transplantation between chip projects of different models: Ze to C8