复杂查询中的函数调用

时间:2017-03-31 14:43:11

标签: oracle plsql


最近有人给我下面的查询以取开价,然后找到下一个价格>开盘价。

这很有效。

当然,他们正在改变我的规格。而不是使用“开放价格”,而是从前一天想要“价格变动百分比”。

我已经有了一个功能,我列出了下面的调用.....但我不知道如何在查询中实现它。我希望有人可以提供帮助。

原始查询

SELECT MAX(price_date), MIN(open_price) KEEP (DENSE_RANK FIRST ORDER BY price_date DESC)
FROM price_history p, master_Table m 
WHERE p.ticker = m.ticker 
AND open_price > (SELECT MIN(open_price) KEEP (DENSE_RANK FIRST ORDER BY price_date DESC) 
                  FROM price_history@ p, master_table m 
                  WHERE p.ticker = m.ticker 
                    AND m.m_ticker = 'IBM') 
AND m.m_ticker ='IBM'; 

调用价格变动百分比功能:

PRICE_FUNCTIONS.NET_CHANGE('IBM', TRUNC(SYSDATE), 1, 'PCT')

样本数据:

IBM 20170330    173.86
IBM 20170329    174.3
IBM 20170328    173.94
IBM 20170327    172.69
IBM 20170324    175.12

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

代替您提供的更多信息,也许这会给您一些关于如何继续的想法?

WITH sample_data AS (SELECT 'IBM' ticker, to_date('30/03/2017', 'dd/mm/yyyy') price_date, 173.86 open_price FROM dual UNION ALL
                     SELECT 'IBM' ticker, to_date('29/03/2017', 'dd/mm/yyyy') price_date, 174.3 open_price FROM dual UNION ALL
                     SELECT 'IBM' ticker, to_date('28/03/2017', 'dd/mm/yyyy') price_date, 173.94 open_price FROM dual UNION ALL
                     SELECT 'IBM' ticker, to_date('27/03/2017', 'dd/mm/yyyy') price_date, 173.69 open_price FROM dual UNION ALL
                     SELECT 'IBM' ticker, to_date('24/03/2017', 'dd/mm/yyyy') price_date, 175.12 open_price FROM dual),
         interim AS (SELECT ticker,
                            price_date,
                            MAX(price_date) OVER (PARTITION BY ticker) max_price_date,
                            open_price,
                            LAG(price_date) OVER (PARTITION BY ticker ORDER BY price_date) prev_price_date,
                            LAG(open_price) OVER (PARTITION BY ticker ORDER BY price_date) prev_open_price
                     FROM   sample_data)
SELECT ticker,
       price_date,
       open_price,
       prev_price_date,
       ROUND(100 * (open_price - prev_open_price) / prev_open_price, 2) percentage_change_from_yday
FROM   interim
WHERE  price_date = max_price_date;

TICKER PRICE_DATE  OPEN_PRICE PREV_PRICE_DATE PERCENTAGE_CHANGE_FROM_YDAY
------ ----------- ---------- --------------- ---------------------------
IBM    30/03/2017      173.86 29/03/2017                            -0.25