如果有商店连续3次减少销售额配置单元或sql中的解决方案?

c86crjj0  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(285)


如果有店铺连续3次减价->打印店铺名称。

python algo:
      1> sort by date.
      loop
      3> select shop(i) if not in list(visited), 
          check if its sale decrease 
          for 3 times consecutively, 
          if yes -> print the shop name.
      2> append shopName in visited.

有没有一种方法可以在hive或sql中实现这一点?

inkz8wg9

inkz8wg91#

在 hive 里,你可以用 lag 以及 lead 获取上一行和下一行的值并使用它们进行比较。

select distinct shop
from (select t.*
      ,lag(sales) over(partition by shop order by date) as prev_sales
      ,lead(sales) over(partition by shop order by date) as next_sales
      from tbl t
     ) t
where prev_sales > sales and sales > next_sales

相关问题