0

假设我有日期和价格列表:

20170322    109.89
20170321    107.02
20170320    109.25
20170317    108.44
20170316    108.53
20170315    107.94
20170314    106.83
20170313    110.02
20170310    107.31
20170309    107.54
20170308    107.67
20170307    108.98

我需要的是,从最近的日期:20170322(109.89)开始,第一个日期/价格值高于原始值,即 20170313(110.02)。请注意,这些是按日期的 DESC 顺序排列的

整天都在。

4

2 回答 2

1

假设这些列被称为 DT 和 PRICE,并假设只有一个“事物”您监控其价格(否则您将需要一个 GROUP BY 子句):

select min(dt) as dt, min(price) keep (dense_rank first order by dt) as price
from   your_table
where  price > ( select min(price) keep (dense_rank first order by dt)
                 from   your_table
               )

https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions056.htm

于 2017-03-24T21:14:52.837 回答
0

使用 MATCH_RECOGNIZE 子句的解决方案(需要 Oracle 12 及更高版本)。

我在 WITH 子句中创建了测试数据。这不是解决方案的一部分;SQL 查询在 WITH 子句之后开始,在SELECT TICKER, ....

PATTERN 中不情愿匹配中的问号会使 JDBC 驱动程序跳闸,因此无法从 SQL Developer 运行此查询;它需要在 SQL*Plus 或类似的前端中运行。(解决方法是更改​​并添加到 DEFINE 子句:b*?。)b*b as b.price <= a.price

为了说明 的更多灵活性MATCH_RECOGNIZE,我假设可能有几个“代码”,每个都有其开始日期(带有价格的最早日期),并且查询查找价格高于原始价格的第一次出现,每个代码.

with
     test_data ( ticker, dt, price ) as (
       select 'XYZ', to_date('20170322', 'yyyymmdd'), 109.89 from dual union all
       select 'XYZ', to_date('20170321', 'yyyymmdd'), 107.02 from dual union all
       select 'XYZ', to_date('20170320', 'yyyymmdd'), 109.25 from dual union all
       select 'XYZ', to_date('20170317', 'yyyymmdd'), 108.44 from dual union all
       select 'XYZ', to_date('20170316', 'yyyymmdd'), 108.53 from dual union all
       select 'XYZ', to_date('20170315', 'yyyymmdd'), 107.94 from dual union all
       select 'XYZ', to_date('20170314', 'yyyymmdd'), 106.83 from dual union all
       select 'XYZ', to_date('20170313', 'yyyymmdd'), 110.02 from dual union all
       select 'XYZ', to_date('20170310', 'yyyymmdd'), 107.31 from dual union all
       select 'XYZ', to_date('20170309', 'yyyymmdd'), 107.54 from dual union all
       select 'XYZ', to_date('20170308', 'yyyymmdd'), 107.67 from dual union all
       select 'XYZ', to_date('20170307', 'yyyymmdd'), 108.98 from dual
     )
select ticker, dt, price
from   test_data
match_recognize (
  partition by ticker
  order     by dt
  measures  c.dt as dt, c.price as price
  one row per match
  pattern   ( ^ a b*? c )
  define    c as c.price > a.price
)
;

TICKER  DT            PRICE
------  ----------  -------
XYZ     2017-03-13   110.02

1 row selected.
于 2017-03-24T22:41:37.147 回答