【问题标题】:MySQL most price change over timeMySQL随着时间的推移价格变化最大
【发布时间】: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


    【解决方案1】:

    我了解到您想计算过去 7 天内每种产品的价格变化了多少次。

    一种天真的方法会使用聚合和count(distinct price) - 但是当产品的价格来回变化时它会失败。

    更安全的方法是窗口函数:您可以使用lag() 检索之前的价格,并将其与当前价格进行比较;然后很容易汇总和计算价格变化:

    select product_id, sum(price <> lag_price) cnt_price_changes
    from (
        select t.*, lag(price) over(partition by product_id order by date) lag_price
        from mytable t
        where date >= current_date - interval 7 day
    ) t
    group by product_id
    order by price_changes desc
    

    【讨论】:

    • 谢谢这很好用。不过想知道,我可以通过price_diff 而不是price_changes 订购吗?我的意思是这个按价格变化的数量排序,如果最大的价格差异先出现然后下降(百分比差异或者只是price - lag_price也许)会很好。可能完全是一个不同的问题..
    • @himmip:查询汇总了每个产品的所有价格变化。假设您想按每个产品的最大价格变化订购:order by max(abs(price - lag_price)) desc
    【解决方案2】:

    尝试改用MAX()MIN()....

    select MAX(pt.price), MIN(pt.price), MAX(pt.price) - MIN(pt.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
    order by change
    

    您可以通过MAX()MIN() 以及最终**MAX() - MIN()** 轻松地找到最大值和最小值,而不是每隔一行减去每一行来获得结果。链接的 MySQL 文档中的相关行...

    MAX():返回expr的最大值。

    MIN():返回expr的最小值。

    您将无法提取其他字段(ID、日期),因为这是MAX()MIN() 隐含的GROUP BY(),但您应该能够通过查询@ 获取该信息987654334@.

    【讨论】:

      【解决方案3】:

      此示例将根据WeekOfYearWeekOfMonth 为您提供关于降低每件产品价格的结果。

      SELECT 
      COUNT(m1.product_id) as total,
      m1.product_id,
      WEEK(m1.ddate) AS weekofyear 
      FROM mytest m1
      WHERE m1.price < (SELECT m2.price FROM mytest m2 WHERE m2.ddate<m1.ddate AND m1.product_id=m2.product_id LIMIT 0,1)
      GROUP BY weekofyear,m1.product_id 
      ORDER BY weekofyear;
      
      SELECT 
      COUNT(m1.product_id) as total,
      m1.product_id,
      FLOOR((DAYOFMONTH(ddate) - 1) / 7) + 1 AS weekofmonth
      FROM mytest m1
      WHERE m1.price < (SELECT m2.price FROM mytest m2 WHERE m2.ddate<m1.ddate AND m1.product_id=m2.product_id LIMIT 0,1)
      GROUP BY weekofmonth,m1.product_id 
      ORDER BY weekofmonth;
      

      SQLFiddle 试试这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-04
        • 2018-09-10
        • 1970-01-01
        • 2019-02-12
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 1970-01-01
        相关资源
        最近更新 更多