现有表,
nm_purchase
字段
id,date,cost (double类型)
现在又数据
1  2010-01-02 08:23:59 23.5
2  2010-01-02 09:23:59 24.5
3  2010-01-03 10:23:59 21.5
4  2010-01-04 11:23:59 26.5
5  2010-01-05 12:23:59 17.5
6  2010-01-06 13:23:59 20.5现在要求取 这一(2010-01-01 到 2010-01-07)段时间中,最早的时间点和最晚的时间点的cost的差;
请各位大侠看清楚条件!谢谢了!

解决方案 »

  1.   

    现在要求取 这一(2010-01-01 00:00:00 到 2010-01-07 23:59:59)段时间中,最早的时间点和最晚的时间点的cost的差;
    谢谢大家了!
      

  2.   

    用row_number() over() 加2部排序字段
    再相减
      

  3.   

    测试数据:CREATE TABLE NMPurchase
    (
        ID NUMBER(3),
        MyDate DATE,
        COST NUMBER(4, 1)
    );
    DELETE T190;
    INSERT INTO NMPurchase VALUES(1, to_date('2010-01-02 08:23:59', 'YYYY-MM-DD HH24:MI:SS'), 23.5);
    INSERT INTO NMPurchase VALUES(2, to_date('2010-01-02 09:23:59', 'YYYY-MM-DD HH24:MI:SS'), 24.5);
    INSERT INTO NMPurchase VALUES(3, to_date('2010-01-03 10:23:59', 'YYYY-MM-DD HH24:MI:SS'), 21.5);
    INSERT INTO NMPurchase VALUES(4, to_date('2010-01-04 11:23:59', 'YYYY-MM-DD HH24:MI:SS'), 26.5);
    INSERT INTO NMPurchase VALUES(5, to_date('2010-01-05 12:23:59', 'YYYY-MM-DD HH24:MI:SS'), 17.5);
    INSERT INTO NMPurchase VALUES(6, to_date('2010-01-06 13:23:59', 'YYYY-MM-DD HH24:MI:SS'), 20.5);
    测试结果:
      

  4.   


    select 
    (select max(cost) from table where date=(select max(date) from table))
    -
    (select max(cost) from table where date=(select min(date) from table)) 
    from dual
      

  5.   

      with t as
    (select 1 id ,to_date('2010-01-02 08:23:59','yyyy-mm-dd hh24:mi:ss') d, 23.5 cost from dual union all
    select 2,to_date('2010-01-02 09:23:59','yyyy-mm-dd hh24:mi:ss')  ,24.5 from dual union all
    select 3 ,to_date('2010-01-03 10:23:59','yyyy-mm-dd hh24:mi:ss')  ,21.5 from dual union all
    select 4 ,to_date('2010-01-04 11:23:59','yyyy-mm-dd hh24:mi:ss')  ,26.5 from dual union all
    select 5 ,to_date('2010-01-05 12:23:59','yyyy-mm-dd hh24:mi:ss')  ,17.5 from dual union all
    select 6 ,to_date('2010-01-06 13:23:59','yyyy-mm-dd hh24:mi:ss')  ,20.5 from dual )
    select  (select cost maxcost from t t2 where t2.d = t1.maxdate ) -(select cost mincost from t t2 where t2.d = t1.mindate )
     from 
    (select max(d) maxdate,min(d) mindate from t where trunc(d,'dd')>= date '2010-01-01' and trunc(d,'dd')<=date '2010-01-07')  t1
      

  6.   


    select ABS((select max(cost) from nm_purchase where date1=(select max(date1) from nm_purchase))-
       (select max(cost) from nm_purchase where date1=(select min(date1) from nm_purchase))) 
    from dual