是这样的,有一个表有casno,sampledate,conc(物质编号,采样时间,浓度)三个字段,其中casno和sampledate为联合主键,共同决定一个浓度值。
我想查询【每种物质在最近的日期的浓度,然后按浓度排序各种物质】,我的思路是【按casno分组,查询最大的sampledate及其对应的conc,最后对conc排序】,所以我写了一个查询select casno, era, max(sampledate)
from xx
group by casno
order by era大家都知道,这明显不对。所以菜鸟请教大家了……我知道很简单的,但自己实在无能为力。谢谢

解决方案 »

  1.   

    select * from tb a
     where not exists(select 1 from tb where casno=a.casno and sampledate>a.sampledate)
    order by era
      

  2.   

    select *
    from xx t
    where sampledate=(select max(sampledate) from xx where casno=t.casno)
    order by conc
      

  3.   

    select a.casno,a.date,q1.conc from (select casno,max(sampledate) as date from xx group by casno) a left join xx on a.casno=q1.casno and a.date=q1.sampledate order by conc好用的话给点分。。1分都木有了