select pol.PO_LINE_ID,pol.UNIT_PRICE,pol.CREATION_DATE
  from po_lines_all pol
 where pol.CREATION_DATE = (select max(pol2.CREATION_DATE) 
                              from po_lines_all pol2 
                          group by pol2.ITEM_ID
                              having pol.ITEM_ID = pol2.ITEM_ID)我想查找对某个Item “创建时间”最晚的那个价格。数据特别的多。我按照上面的去跑半个小时都跑不出来谁帮忙优化一下

解决方案 »

  1.   

    试试这个--select pol.PO_LINE_ID,pol.UNIT_PRICE,pol.CREATION_DATE
    --  from po_lines_all polwith t as (
    select 1 as id , 100 as price, sysdate as fdate from dual
    union all 
    select 1,101,sysdate + 1 from dual
    union all 
    select 2,110,sysdate  from dual
    union all 
    select 2,111,sysdate+1  from dual
    )
    select id,price,fdate from (
    select id,price,fdate,rank()over(partition by id order by fdate desc) as rankNum from t
    ) where rankNum = 1
      

  2.   

    感觉你思路就不对!
    首先要明白慢在哪一步?
    我的思路是:
    1.你是知道具体的item_id的,所以最先做的是:
    select pol2.ITEM_ID,max(pol2.CREATION_DATE)  
      from po_lines_all pol2  where pol2.item_id =?
      group by pol2.ITEM_ID
      having pol.ITEM_ID = pol2.ITEM_ID
    加个条件,这样就不会整个表里去分组,建立item_id的索引!
      

  3.   

    select id,price,fdate from (
    select id,price,fdate,rank()over(partition by id order by fdate desc) as rankNum from t
    ) where rankNum = 1这个是最好的。