select * from tbname where (type,date) in(
select type,max(date) from tbname group by type);

解决方案 »

  1.   

    顺便问一下,如何同时查统计和明细:
    以上面的表为例,现在要同时查各个type的value累计值和最新值
    即结果集如下:
    type     total   value
    --------------------
    1         7       4
    2         5       5
    3         13       7
      

  2.   

    select t.type,t.date,t.value,tt.total from 
    (select  *  from  tbname  where  (type,date)  in(  
    select  type,max(date)  from  tbname  group  by  type)
    ) t,
    (select type,sum(value) sv from tbname group by type
    ) tt
    where t.type=tt.type(+);
      

  3.   

    --测试:
    create table table1( typ number,dat varchar2(10),val number);
    insert into table1 values(1,'2004-8-1',3);
    insert into table1 values(1,'2004-8-2',4);
    insert into table1 values(2,'2004-7-31',5);
    insert into table1 values(3,'2004-3-1',6);
    insert into table1 values(3,'2004-4-1',7);
    select * from table1 a 
    where dat in (select max(dat) from table1 where typ = a.typ);
    /*
           TYP DAT               VAL
    ---------- ---------- ----------
             1 2004-8-2            4
             2 2004-7-31           5
             3 2004-4-1            7已选择3行。
    */select a.*,(select sum(val) from table1 where typ = a.typ) as total 
    from table1 a  
    where dat in (select max(dat) from table1 where typ = a.typ);
    /*       TYP DAT               VAL      TOTAL
    ---------- ---------- ---------- ----------
             1 2004-8-2            4          7
             2 2004-7-31           5          5
             3 2004-4-1            7         13已选择3行。
    */