mysql检查一天的记录与不同日期的记录“同店销售”

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

同店销售概念,如果你检查商店今天的表现与昨天相同的商店,以显示收入增长或减少。所以我有一张table 5+ millions 记录的结构像

store_id , stats_date, trans_cnt (number of transactions), 
revenue, time_period(week, day , year)

有没有办法避免使用 cursor ,以检查 store_id 记录昨天和今天的收入,看看收入是上升还是下降?

z4iuyo4d

z4iuyo4d1#

它可以从表或子表中实现对同一过滤数据的连接。
ie公司

select tdate.store_id  ,(tdate.revenue - ydate.revenue) as diffrence 
 from (select store_id ,revenue from tablename where stats_date =getdata()) tdate 
   join ( select store_id ,revenue from tablename  where stats_date = DATEADD(day, -1,getdata()) )  as ydate
  on tdate.store_id = ydate.store_id

注:
昨天的筛选数据
t昨天的筛选数据
可以添加更多的过滤条件。
或者你在找

select tdate.store_id  ,(tdate.revenue - ydate.revenue) as diffrence 
   from  tablename tdate 
   join tablename   as ydate
    on tdate.store_id = ydate.store_id
    and tdate.stats_date =ydate.DATEADD(day, -1,stats_date)

相关问题