Oracle分析查询

时间:2016-09-22 16:56:30

标签: sql oracle

所有,我需要一些帮助来编写查询,这里是剪切数据......

TABLE_NAME                                SIZE_BYTES      DATE
----------------------------------------  --------------- ---------
TABLE_A                                   346,817,560,576 17-SEP-16         
TABLE_A                                   346,817,560,576 18-SEP-16         
TABLE_A                                   369,046,323,200 19-SEP-16         
TABLE_A                                   413,623,386,112 20-SEP-16         
TABLE_A                                   466,840,715,264 21-SEP-16
TABLE_B                                   42895409152     17-SEP-16
TABLE_B                                   42962518016     18-SEP-16
TABLE_B                                   43163844608     19-SEP-16
TABLE_B                                   43365171200     20-SEP-16
TABLE_B                                   43566497792     21-SEP-16    

17-SEP-2016是基线,每天计算增长,因此在19-SEP-16数据上增加了19-SEP-2016 size_bytes减去18-SEP-16 size_bytes。

我希望实现的目标是以这种格式显示数据

TABLE_NAME    DATE        SIZE_DELTA
------------- ----------- --------------
TABLE_A       17-SEP-16 346,817,560,576
              18-SEP-16 0
              19-SEP-16 22,228,762,624
              20-SEP-16 44,577,062,912
              21-Sep-16 53,217,329,152
TABLE_B       17-Sep-16 42895409152
              18-Sep-16 67108864
              19-Sep-16 201326592
              20-Sep-16 201326592
              21-Sep-16 201326592   

1 个答案:

答案 0 :(得分:2)

使用LAG功能:https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions082.htm#SQLRF00652

SELECT table_name, date,
       size_bytes - lag( size_bytes, 1, 0 ) 
                    OVER ( partition by table_name order by date ) As SIZE_DELTA
FROM tablename;