over 之rows 与range窗口区别

select * from t order by t.area_code,t.bill_month;
-- 逻辑范围  根据order by 中的字段计算逻辑上的范围
/*
如下表中数据实际只有5,6,8月分的
月分   计算范围  实际
5      4+5+6     5+6
6      5+6+7     5+6
8      7+8+9     8
*/
select area_code,bill_month,sum(countId) over(partition by area_code order by bill_month range between 1 preceding and 1 following)
 from(
select 
       t.area_code,t.bill_month,count(area_code) as countId
       from t group by t.area_code,t.bill_month
       
       );
 -- 物理范围,根据行找      
select area_code,bill_month,sum(countId) over(partition by area_code order by bill_month rows between 1 preceding and 1 following)
 from(
select 
       t.area_code,t.bill_month,count(area_code) as countId
       from t group by t.area_code,t.bill_month
       
       );

 

create table t(
       bill_month number, 
       area_code number,
       net_type char, 
       local_fare number
);


declare
  i      number;
  period number := 200405;
  a_code number := 5060;
  v_type char(1) := 'G';
  num    number;
  k      number;
begin
  
  for k in 0 .. 3 loop
    period := period+k;
    a_code := 5060;
    for i in 0 .. 9 loop
      if mod(i, 2) = 0 then
        a_code := a_code + 1;
      end if;
      if (mod((i + 1), 2) != 0) then
        v_type := 'J';
      end if;
      if (mod((i + 1), 2) = 0) then
        v_type := 'G';
      end if;
      num := DBMS_RANDOM.RANDOM;
      num := num / 100;
      if num < 0 then
        num := -1 * num;
      end if;
      insert into t
        (bill_month, area_code, net_type, local_fare)
      values
        (period, a_code, v_type, num);
    end loop;
  end loop;
  commit;

end;

 

range前的order by 字段只能是数字吗?
转成日期比较会出错,那要统计如“201001”时,就统计不到200912了,这种情况该如何解决

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值