当前位置:网站首页>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
边栏推荐
- 洛谷P3236 [HNOI2014]画框 题解
- 21 days learning mongodb notes
- Record Alibaba cloud server mining program processing
- V-model binding value in El select, data echo only displays value, not label
- leetcode:437. 路径总和 III【dfs 选还是不选?】
- NPDP|产品经理如何做到不会被程序员排斥?
- leetcode-791. Custom string sorting
- Process virtual address space partition
- Record the problems encountered in using v-print
- C, calculation code of parameter points of two-dimensional Bezier curve
猜你喜欢

Teach you to quickly develop a werewolf killing wechat applet (with source code)

教你快速开发一个 狼人杀微信小程序(附源码)

Record Alibaba cloud server mining program processing

Remote sensing image classification and recognition system based on convolutional neural network

免费试用一个月的服务器,并附上教程

Learning materials

5 free audio material websites, recommended collection

Deploying MySQL in cloud native kubesphere

Redis deployment of cloud native kubesphere

STM32 control stepper motor (ULN2003 + 28byj)
随机推荐
Buuctf Web [bjdctf2020] zjctf, but so
Labels and paths
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
解锁OpenHarmony技术日!年度盛会,即将揭幕!
World Book Day: I'd like to recommend these books
[daily question] chessboard question
Recommended website for drawing result map
SSM框架系列——数据源配置day2-1
BUUCTF WEB [BUUCTF 2018]Online Tool
21 天学习MongoDB笔记
STM32 project transplantation: transplantation between chip projects of different models: Ze to C8
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
数据库中的日期时间类型
梳理網絡IP代理的幾大用途
标签与路径
Introduction to servlet listener & filter
CGC: contractual graph clustering for community detection and tracking
Free and open source charging pile Internet of things cloud platform
Homomorphic encryption technology learning
大家帮我看一下这是啥情况,MySQL5.5的。谢了