表结构如下 
message 表 
id(varchar2)  receivetime(date) taskid projectid(varchar2) 
    1              2009-1-1      1    1 
    2              2009-1-2      1    1 
    3              2009-1-1      1    2 
    4              2009-1-3      2    2 现在想根据projectId分组,取出每个projectId里面receivtime的最大时间,并且返回这个表的所有字段, 
例如根据以上要求,上面的结果为: 
    id(varchar2)  receivetime(date) taskid projectid(varchar2) 
    2              2009-1-2            1        1 
    4              2009-1-3            2        2 
怎么弄呀,急急急 

解决方案 »

  1.   

    select t1.id,t1.receivetime,t1.taskid,t1.projectid,t1..... from message t1,(select projectid,max(receivetime)  receivetime from message group by projectid) t2 where t1.projectid=t2.projectid and t1.receivetime=t2.receivetime
      

  2.   

    http://topic.csdn.net/u/20091117/12/d90b6a48-1b00-4867-a59d-c1282d26d33a.html?73903
      

  3.   

    select max(id),max(receivetime),max(taskid),projectid
          from tmp_message  a
          group by projectid
      

  4.   

    create table #message(id varchar(20),receivetime varchar(10), taskid varchar(10),projectid varchar(10))insert into #message
    select 1,'2009-1-1',1,1
    union all
    select 2,'2009-1-2',1,1 union all
    select 3,'2009-1-1',1,2 union all
    select 4,'2009-1-3',2,2 union all
    select 5,'2009-1-1',2,2  
     
    select *from #message a where not exists(select 1 from #message b where b.receivetime>a.receivetime and  b.projectid=a.projectid)
     --以上环境MSSQL.不好意思!
      

  5.   

    根据projectId分组,取出每个projectId里面receivtime的最大时间,并且返回这个表的所有字段刚才那个帖子不是答了么,还有其他问题?
      

  6.   

    可以用下面的SQL试一下,估计可以结贴了
    Select *
      From message
     Where id In
           (Select id
              From (Select id, Max(receivetime) From message Group By id) A)