Oracle查询使用条件以获取特定日期

时间:2018-09-26 12:16:12

标签: sql oracle

我有一张价格表,如下所示:

PRODUCT_ID  PRICE_DATE  PRICE  LAST_PRICE
BlueLotion  24/08/2018  £10.00  £7.50  
BlueLotion  23/08/2018  £10.00  £7.50 
BlueLotion  22/08/2018  £10.00  £7.50  
BlueLotion  21/08/2018  £7.50   £6.50   
BlueLotion  20/08/2018  £7.50   £6.50   
BlueLotion  19/08/2018  £7.50   £6.50 
BlueLotion  17/08/2018  £6.50   £7.50 
BlueLotion  16/08/2018  £6.50   £7.50 
BlueLotion  13/08/2018  £6.50   £7.50   
BlueLotion  12/08/2018  £7.50   NULL       
BlueLotion  11/08/2018  £7.50   NULL        
BlueLotion  10/08/2018  £7.50   NULL        

在编写查询以获取每个记录的最后价格日期时,我需要一些帮助。例如,在数据行1中,BlueLotion的最后价格为7.5,而该价格最后一次于2018年8月21日

因此,结果集如下所示:

PRODUCT_ID  PRICE_DATE  PRICE  LAST_PRICE  DATE_WITH_PREV_RATE
BlueLotion  24/08/2018  £10.00  £7.50       21/08/2018
BlueLotion  23/08/2018  £10.00  £7.50       21/08/2018
BlueLotion  22/08/2018  £10.00  £7.50       21/08/2018
BlueLotion  21/08/2018  £7.50   £6.50       17/08/2018
BlueLotion  20/08/2018  £7.50   £6.50       17/08/2018
BlueLotion  19/08/2018  £7.50   £6.50       17/08/2018
BlueLotion  17/08/2018  £6.50   £7.50       12/08/2018
BlueLotion  16/08/2018  £6.50   £7.50       12/08/2018
BlueLotion  13/08/2018  £6.50   £7.50       12/08/2018
BlueLotion  12/08/2018  £7.50   NULL        NULL
BlueLotion  11/08/2018  £7.50   NULL        NULL
BlueLotion  10/08/2018  £7.50   NULL        NULL 

为帮助该示例,请参见以下脚本来构建表。

create table COMP_RESULTS (product_id varchar2(20), price_date date, product_price number, last_price number);
insert into comp_results values ('BlueLotion',DATE '24 AUG 2018','10','7.5');
insert into comp_results values ('BlueLotion',DATE '23 AUG 2018','10','7.5');
insert into comp_results values ('BlueLotion',DATE '22 AUG 2018','10','7.5');
insert into comp_results values ('BlueLotion',DATE '21 AUG 2018','7.5','6.5');
insert into comp_results values ('BlueLotion',DATE '20 AUG 2018','7.5','6.5');
insert into comp_results values ('BlueLotion',DATE '19 AUG 2018','7.5','6.5');
insert into comp_results values ('BlueLotion',DATE '18 AUG 2018','6.5','7.5');
insert into comp_results values ('BlueLotion',DATE '17 AUG 2018','6.5','7.5');
insert into comp_results values ('BlueLotion',DATE '15 AUG 2018','6.5','7.5');
insert into comp_results values ('BlueLotion',DATE '14 AUG 2018','7.5',NULL);
insert into comp_results values ('BlueLotion',DATE '13 AUG 2018','7.5',NULL);
insert into comp_results values ('BlueLotion',DATE '12 AUG 2018','7.5',NULL);

注意:表中不仅有BlueLotion,还有其他产品。

2 个答案:

答案 0 :(得分:2)

您可以尝试使用LAG功能:https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=a545cab41ec5858e4f54f9220eb8c4f4

 select comp_results.*,lastchangeddate from comp_results 
      inner join
    (select product_id,prev_price,min(case when product_price<>prev_price then price_Date end) as lastchangeddate 
    from 
    (
     select product_id,price_Date, product_price,last_price,
     LAG(product_price, 1, 0) OVER (ORDER BY price_Date desc) as prev_price
     from comp_results)a  where prev_price<>0 group by product_id,prev_price
   )b 
    on comp_results.product_id =b.product_id and 
    comp_results.product_price=b.prev_price
    order by price_Date desc

答案 1 :(得分:0)

您可以使用 correlated 子查询:

SELECT cr.*,
       (SELECT c1.PRICE_DATE
        FROM COMP_RESULTS cr1
        WHERE cr1.PRODUCT_ID = cr.PRODUCT_ID AND 
              cr1.PRICE = cr.LAST_PRICE AND cr1.PRICE_DATE < cr.PRICE_DATE
        ORDER BY c1.PRICE_DATE DESC
        FETCH FIRST 1 ROWS ONLY
       ) AS DATE_WITH_PREV_RATE
FROM COMP_RESULTS cr;