请教个查询SQL语句ID PRO_ID VALUE DATE_TIMER
1 1000 90 2009-4-1 12:00:00
2 1000 100 2009-4-2 12:00:006 1003 90 2009-4-1 12:00:00
7 1003 91 2009-4-2 12:00:00
8 1003 100 2009-4-3 12:00:00
想要显示如下结果:2 1000 100 2009-4-2 12:00:00
8 1003 100 2009-4-3 12:00:00以PRO_ID为主,取出DATE_TIMER最大值的数据

解决方案 »

  1.   

    select max(id),pro_id,max(value),max(date_timer) from table group by pro_id
      

  2.   

    select distictc(id),pro_id, date_timer from tb order by id asc , date_timer desc  
      

  3.   


    我看楼主需要样例数据,感觉是以id为主,不是以pro_id为主,所以group by是以id来查询的:1, select distictc(pro_id),value, date_timer from tb order by pro_idasc , date_timer desc  2, select pro_id,max(value),max(date_timer) from table group by pro_id 
      

  4.   


    看错了,是以pro_id为主。
      

  5.   


    select ID,PRO_ID, VALUE, DATE_TIMER from 
    (
    select ID,PRO_ID, VALUE, DATE_TIMER ,row_number() over(PARTITION  BY PRO_ID order by PRO_ID, DATE_TIMER desc) as rn
      from table1
    order by PRO_ID, DATE_TIMER 
    )
    where rn=1
      

  6.   

    system@ORCL(192.168.21.110)> select * from test_t;        ID     PRO_ID      VALUE DATE_TIMER
    ---------- ---------- ---------- --------------
             1       1000         90 01-4月 -09
             2       1000        100 02-4月 -09
             6       1003         90 01-4月 -09
             7       1003         91 02-4月 -09
             8       1003        100 03-4月 -09已用时间:  00: 00: 00.03
    system@ORCL(192.168.21.110)> select id,pro_id,value,date_timer
      2  from
      3  (
      4  select id,pro_id,value,date_timer,
      5  row_number() over(partition by pro_id order by value desc) recno from test_t
      6  )
      7  where recno=1;        ID     PRO_ID      VALUE DATE_TIMER
    ---------- ---------- ---------- --------------
             2       1000        100 02-4月 -09
             8       1003        100 03-4月 -09已用时间:  00: 00: 00.03
      

  7.   

    看错了,应该是order by DATE_TIMER desc
      

  8.   

    SELECT A.*
      FROM TABLE A,
           (SELECT A.PRO_ID, MAX(A.DATE_TIMER) DATE_TIMER
              FROM TABLE A
             GROUP BY A.PRO_ID) B
     WHERE A.PRO_ID = B.PRO_ID
       AND A.DATE_TIMER = B.DATE_TIMER
      

  9.   


    select * from tb t1 where 
    t1.PRO_ID = (select PRO_ID from tb t2 where t1.PRO_ID=t2.PRO_ID group by PRO_ID)
    and
    t1.DATE_TIMER=(select max(DATE_TIMER) from tb t3 where t1.PRO_ID=t3.PRO_ID group by PRO_ID)