4

请教一个极值差值且最小值需在最大值创建时间前的问题

 3 years ago
source link: https://www.v2ex.com/t/787298
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  MySQL

请教一个极值差值且最小值需在最大值创建时间前的问题

  Te11UA · 1 天前 · 226 次点击

基于 Mysql 5.7 ,想要做的效果是查看商品上升排名最多的商品

表结构如下,每 10 分钟打点一次,记录商品排名:

create table product_info
(
    prodcut_name          varchar(16)                        null comment '商品名',
    total_index   smallint                           null comment '总排名',
    create_time   datetime default CURRENT_TIMESTAMP not null comment '创建时间'
);
SELECT prodcut_name, max(total_index) - min(total_index) AS index_diff
FROM product_info
WHERE create_time > '2021-07-01 00:00:00'
GROUP BY prodcut_name
ORDER BY index_diff DESC
limit 10;

但是发现这个只是考虑了差值,没有考虑到最大值的时间必须在最小值的时间前,这样写会下降排名最多的也会显示。这下 SQL 语句不会写了,思来想去只能分个语句写,想请问下有没有可写在一条的语句呢?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK