“同店销售”概念,如果您在此处检查今天的商店相对于昨天的同店的表现如何,以显示收入增长还是减少。
所以我有5+ millions
记录的表,其结构类似于
store_id , stats_date, trans_cnt (number of transactions),
revenue, time_period(week, day , year)
有没有一种方法可以避免使用cursor
,来检查昨天和今天的store_id
记录是否存在,并查看收入是上升还是下降?
答案 0 :(得分:1)
可以实现对表或子表中相同过滤器数据的联接。
即
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
注意:
昨天的日期过滤器数据
昨天的tdate过滤器数据
可以添加更多过滤条件。
或者您正在寻找
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)