简体   繁体   English

plqsl 复制 vlookup

[英]plqsl replicating vlookup

My query returns the following table:我的查询返回下表:

|  CODE  |    DATE    |  VALUE  |
|  123   | 30/06/2012 |   11    |
|  231   | 01/07/2012 |   22    |
|  321   | 02/07/2012 |   11    |
|  234   | 03/07/2012 |   11    |

I need it to create a column based off of the DATE and VALUE column.我需要它根据 DATE 和 VALUE 列创建一个列。 So it must take todays value ad divide it by the previous days所以必须用今天的价值除以前几天

So once the query runs the result should be:所以一旦查询运行,结果应该是:

|  CODE  |    DATE    |  VALUE  |  RET  |
|  123   | 30/06/2012 |   11    |       |
|  231   | 01/07/2012 |   22    |  2.0  |
|  321   | 02/07/2012 |   11    |  0.5  |
|  234   | 03/07/2012 |   11    |  0.0  |

This is as far as I have got:这是据我所知:

SELECT
    CODE
    DATE
    VALUE 
    (DATE/(TO_DATE(DATE)-1)) AS RET
FROM
    ....

But obviously you can't divide a date by another date and expect to get a number.但显然你不能将一个日期除以另一个日期并期望得到一个数字。

Thanks谢谢

To be able to access previous value of a column you can use lag analytical function:为了能够访问列的先前值,您可以使用滞后分析函数:

   -- sample of data from your question
   SQL> with t1(CODE, DATE1, VALUE1) as(
      2    select 123, to_date('30/06/2012', 'dd/mm/yyyy'), 11  from dual union all
      3    select 231, to_date('01/07/2012', 'dd/mm/yyyy'), 22  from dual union all
      4    select 321, to_date('02/07/2012', 'dd/mm/yyyy'), 11  from dual union all
      5    select 234, to_date('03/07/2012', 'dd/mm/yyyy'), 11  from dual
      6  )-- the query
      7  select code
      8       , Date1
      9       , Value1
     10       , to_char(value1 / lag(value1, 1) over(order by date1), '999990.0') ret
     11    from t1
     12  ;

Result:结果:

      CODE DATE1           VALUE1      RET
---------- -----------   ----------   ---------
       123 30/06/2012         11 
       231 01/07/2012         22       2.0
       321 02/07/2012         11       0.5
       234 03/07/2012         11       1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM