group by 的问题,急急急

tyjohn 2009-11-17 01:21:40
表结构如下
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
怎么弄呀,急急急
...全文
90 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
无忧工作室 2009-11-17
  • 打赏
  • 举报
回复
可以用下面的SQL试一下,估计可以结贴了
Select *
From message
Where id In
(Select id
From (Select id, Max(receivetime) From message Group By id) A)

[Quote=引用楼主 tyjohn 的回复:]
表结构如下
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
怎么弄呀,急急急
[/Quote]
shiyiwan 2009-11-17
  • 打赏
  • 举报
回复
根据projectId分组,取出每个projectId里面receivtime的最大时间,并且返回这个表的所有字段

刚才那个帖子不是答了么,还有其他问题?
liusong_china 2009-11-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tyjohn 的回复:]
恩,3楼正解,结贴了
[/Quote]

3楼的句子是有问题的。。。。。。。
tyjohn 2009-11-17
  • 打赏
  • 举报
回复
恩,3楼正解,结贴了
mir2ming 2009-11-17
  • 打赏
  • 举报
回复
其实我是搞JAVA的,路过就帮写了。。。热心人真多啊
duanzhi1984 2009-11-17
  • 打赏
  • 举报
回复
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.不好意思!
wwwtyb 2009-11-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mir2ming 的回复:]
select max(id),max(receivetime),max(taskid),projectid
      from tmp_message  a
      group by projectid
[/Quote]
正解.........
mir2ming 2009-11-17
  • 打赏
  • 举报
回复
select max(id),max(receivetime),max(taskid),projectid
from tmp_message a
group by projectid
crazylaa 2009-11-17
  • 打赏
  • 举报
回复
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

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧