zoukankan      html  css  js  c++  java
  • 当年/月/日累计指标数据初始化

     1 -- 当年/月/日累计指标数据初始化
     2 with data_day as (
     3     select 'a' name,'2016-07-01' as day, 1  as value union all
     4     select 'a' name,'2017-07-01' as day, 2  as value union all
     5     select 'a' name,'2018-05-01' as day, 3  as value union all
     6     select 'a' name,'2018-06-01' as day, 4  as value union all
     7     select 'a' name,'2018-07-01' as day, 5  as value union all
     8     select 'a' name,'2018-07-02' as day, 6  as value union all
     9     select 'a' name,'2018-07-03' as day, 7  as value union all
    10     select 'a' name,'2018-08-01' as day, 8  as value union all
    11     select 'a' name,'2018-08-03' as day, 9  as value union all
    12     select 'a' name,'2018-09-01' as day, 10 as value union all
    13     select 'a' name,'2018-09-02' as day, 11 as value union all
    14     select 'a' name,'2018-09-03' as day, 12 as value
    15 )
    16 ,cal as (
    17     select '2018-07-01' as day union all
    18     select '2018-07-02' as day union all
    19     select '2018-07-03' as day union all
    20     select '2018-08-01' as day union all
    21     select '2018-08-02' as day union all
    22     select '2018-08-03' as day union all
    23     select '2018-09-02' as day union all
    24     select '2018-09-03' as day
    25 )
    26 select
    27      t2.name
    28     ,t1.day
    29     ,sum(case when t1.day = t2.day                         then value end) as value_d -- 当日数量
    30     ,sum(case when trunc(t1.day,'MM') = trunc(t2.day,'MM') then value end) as value_m -- 当月累计
    31     ,sum(case when trunc(t1.day,'YY') = trunc(t2.day,'YY') then value end) as value_y -- 当年累计
    32     ,sum(value)                                                            as value_t -- 全量累计
    33 from cal t1
    34 join data_day t2
    35     on 1 = 1
    36 where t2.day <= t1.day
    37 group by
    38      t2.name
    39     ,t1.day
    40 order by
    41      t2.name
    42     ,t1.day
    43 ;
    +-------+-------------+----------+----------+----------+----------+--+
    | name  |     day     | value_d  | value_m  | value_y  | value_t  |
    +-------+-------------+----------+----------+----------+----------+--+
    | a     | 2018-07-01  | 5        | 5        | 12       | 15       |
    | a     | 2018-07-02  | 6        | 11       | 18       | 21       |
    | a     | 2018-07-03  | 7        | 18       | 25       | 28       |
    | a     | 2018-08-01  | 8        | 8        | 33       | 36       |
    | a     | 2018-08-02  | NULL     | 8        | 33       | 36       |
    | a     | 2018-08-03  | 9        | 17       | 42       | 45       |
    | a     | 2018-09-02  | 11       | 21       | 63       | 66       |
    | a     | 2018-09-03  | 12       | 33       | 75       | 78       |
    +-------+-------------+----------+----------+----------+----------+--+
    1 select
    2      day
    3     ,sum(value) over(partition by trunc(day,'MM') order by day asc) as value_asc1                                              -- 当月累计
    4     ,sum(value) over(partition by trunc(day,'MM') order by day rows between unbounded preceding and current row) as value_asc2 -- 当月累计
    5 from (
    6     select '2018-08-01' as day ,100 as value union all
    7     select '2018-08-03' as day ,100 as value
    8 ) t
    9 ;
    +-------------+-------------+-------------+--+
    |     day     | value_asc1  | value_asc2  |
    +-------------+-------------+-------------+--+
    | 2018-08-01  | 100         | 100         |
    | 2018-08-03  | 200         | 200         |
    +-------------+-------------+-------------+--+
  • 相关阅读:
    升级windows 11小工具
    windows 10更新升级方法
    您需要了解的有关 Oracle 数据库修补的所有信息
    Step by Step Apply Rolling PSU Patch In Oracle Database 12c RAC Environment
    Upgrade Oracle Database Manually from 12.2.0.1 to 19c
    如何应用版本更新 12.2.0.1.210420(补丁 32507738 – 2021 年 4 月 RU)
    xtrabackup 安装、备份和恢复
    Centos_Lvm expand capacity without restarting CentOS
    Centos_Lvm_Create pv vg lv and mount
    通过全备+relaylog同步恢复被drop的库或表
  • 原文地址:https://www.cnblogs.com/chenzechao/p/9553857.html
Copyright © 2011-2022 走看看