表结构如下:
   字段名:obj_id       test_date       test_value
CQcZIZk19w 2012-7-1 15.68
CQLmQmjtAy 2012-7-2 10.13
CQcZIZk19w 2012-7-5 5.54
CQIxSKeoJg 2012-7-6 7.24
CQRkZPGNbn 2012-7-22 7.24
CQUsKueDT8 2012-7-22 10.13
CQjAeFodFS 2012-7-24 7.24
CQcZIZk19w 2012-7-24 10.13
CQ1sfsYsXK 2012-7-26 7.24
CQIxSKeoJg 2012-7-27 7.24
obj_id记录会有重复,test_date也会有重复,要求能取得每一个OBJ_ID对应最大日期test_date的test_value.
表记录有10万多条!

解决方案 »

  1.   

    这个是我写的,但是感觉不是多优化的
    select a.obj_id,b.test_valuefrom
    (select t.obj_id, max(test_date) test_date   from mech_all t
    where 
    t.test_value is not null
    group by obj_id) A,
    (select t.obj_id, test_date,T.test_value from mech_all t
    where 
    t.test_value is not null) B
    WHERE a.obj_id=b.obj_id and a.test_Date=b.test_date
    十万多条记录,执行的效率不咋样!
      

  2.   

    select t.obj_id,t.test_value
    from mech_all t
    WHERE t.test_date =  (select max(test_date)
        from mech_all t1
         where 
           t1.test_value is not null
          and t1.obj_id = t.obj_id ) 你用这个SQL试试   最好这种大数据的表不要做子查询(临时表)
    全表检索  消耗很大的  对性能影响很大