限制SQL中的分组

时间:2015-09-21 17:53:29

标签: sql netezza

我有一些代码可以计算销售额随时间的平均值,然后衡量最后4次销售情况,看看它们是否低于此基线的20%。我让它一次为一个客户工作,但我需要它为所有客户工作。

select cust_nbr, avg(b) avg_baseline, sales from (
  select cust_nbr, baseline as b, sales from (
    select tbl.*,
       avg(sales) over(partition by cust_nbr order by cust_nbr, weekly rows between 12 preceding and current row) as rolling_avg, (rolling_avg*0.2) as baseline
     from (
       select cust_nbr, sum(sales) as sales, next_day(prcs_dt,'Sunday') - 1 as weekly from table
       where cust_nbr in ('1234')--, '2345', '3456')
       group by cust_nbr, weekly
     ) tbl
   ) tbl2
  order by weekly desc limit 4  --Limiting to 4 rows, need it to be 4 per group
) tbl3 group by cust_nbr, sales

当前输出:

cust_nbr, avg_baseline, sales
1234, 20, 150
1234, 21, 160
1234, 23, 180
1234, 25, 140

理想输出:

cust_nbr, flag
1234, 0

其中标志是客户的销售额是否低于过去4次销售的滚动平均值的20%

1 个答案:

答案 0 :(得分:0)

您使用var orig = {text:""}; $('.class').on('click','.etc',function(){ if($a == "Edit") // just checking if edit button { orig.text = $(this).find('original-text').text(); } else if ($a == "Cancel") { alert(orig.text); } }); 子句限制group by。它就像having子句,但它与where一起使用。