zoukankan      html  css  js  c++  java
  • over partition by

    题目一:

    现有这么一批数据,现要求出:
    每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数

     建表

    create table TABLE_0111
    (
      NAME  VARCHAR2(20),
      MONTH VARCHAR2(20),
      PV    INTEGER
    )
    

    准备数据

    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-01', 5);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-01', 15);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-01', 5);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-01', 8);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-01', 25);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-01', 5);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-02', 4);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-02', 6);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-02', 10);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-02', 5);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-03', 16);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('A', '2015-03', 22);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-03', 23);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-03', 10);
    
    insert into TABLE_0111 (NAME, MONTH, PV)
    values ('B', '2015-03', 11);
    

    第一种思路:通过内连接的方式得出答案

    create or replace view tab_01 as
      select name, month, sum(pv) as total
        from TABLE_0111
       group by name, month
       order by name, month;
    
    create or replace view tab_02 as
      select *
        from (select t1.name  as t1name,
                     t1.month as t1month,
                     t1.total as t1total,
                     t2.name  as t2name,
                     t2.month as t2month,
                     t2.total as t2total
                from tab_01 t1
                join tab_01 t2 on t1.name = t2.name)
       where t2month >= t1month;
    
    select * from tab_01;
    select * from tab_02;
    select t2name, t2month, t2total, max(t1total), sum(t1total)
      from tab_02
     group by t2name, t2month, t2total
     order by t2name, t2month;
    

    第二种思路:通过分析函数

    select * from TABLE_0111;
    
    select name,
           month,
           total,
           max(total) over(partition by name order by name, month rows between unbounded preceding and current row) as maxpv,
           sum(total) over(partition by name order by name, month rows between unbounded preceding and current row) as sumpv
      from tab_01;
    

      

     

     

  • 相关阅读:
    LOJ-10108(欧拉回路+并查集)一个图至少用几笔画成
    hdu-1878(欧拉回路)
    LOJ-10106(有向图欧拉回路的判断)
    欧拉回路
    LOJ-10105(欧拉回路模板,套圈法,递归)
    LOJ-10102(求A到B之间的割点)
    LOJ-10103(求删去割点后最多的连通分量)
    LOJ-10102(桥的判断)
    【XSY2278】【HDU5669】the Red Sun(线段树+dijkstra)
    【XSY2434】【CF787D】遗产(线段树+dijkstra)
  • 原文地址:https://www.cnblogs.com/jycjy/p/11582482.html
Copyright © 2011-2022 走看看