滞销品的定义需要明确:而不是“一个月”或“多个月”这种不确定的定义。
SQL语句不能应付这种“不确定”的情况,除非你将其当参数写成函数!

解决方案 »

  1.   

    select t.id, sum(sale_num) sale_num
      from t
     where sale_date between start_date and end_date
     group by t.id
    having sum(sale_num) <=10根据具体情况修改吧
      

  2.   

    start_date 和end_date 你自己算就是了
      

  3.   


    我也接着优化下:
    select t.id, sum(t.sale_num)
      from (select r.id,
                   r.sale_date,
                   min(r.sale_date) over(partition by r.id) as min_date,
                   r.sale_num
              from web_clm_rule r) t
     where t.sale_date between t.min_date and t.min_date + 90
     group by t.id
    having sum(t.sale_num) <= 10