【发布时间】:2021-01-07 22:28:27
【问题描述】:
price | date | product_id
100 | 2020-09-21 | 1
400 | 2020-09-20 | 2
300 | 2020-09-20 | 3
200 | 2020-09-19 | 1
400 | 2020-09-18 | 2
我每天都会在此表中添加一个条目,其中包含当天产品的价格。
现在我想获得上周的大多数降价(所有日期都在 2020 年 9 月 14 日之前),在这个例子中它只会返回 product_id = 1,因为这是唯一改变的东西。
我认为我必须将表加入到自身中,但我无法让它工作。
这是我想返回过去一天价格变化最多的东西,但它不起作用。
select pt.price, pt.date, pt.product_id, (pt.price - py.price) as change
from prices as pt
inner join (
select *
from prices
where date > '2020-09-20 19:33:43'
) as py
on pt.product_id = py.product_id
where pt.price - py.price > 0
order by change
【问题讨论】:
标签: mysql sql datetime count window-functions