表结构如下
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
怎么弄呀,急急急
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
怎么弄呀,急急急
解决方案 »
- 创建oracle用户
- 【请教高高手】如何在PL/SQL环境里直接使用已经创建的PRC、PKG、FUN等,请老师们指教???
- oci的内置数据类型和扩展数据类型 有啥区别
- 急!! 在PL/SQL 中,怎么生成14位的整数 ?
- 读取指定分隔符的问题
- Oracle中创建用户有什么要求吗?
- 小弟寻找 吉林大学 的oracle 视频教程??? 非常感谢.....
- oracle存储过程读xls,并插入数据到数据库
- oracle8.1.6安装及删除问题?
- 当要插入的数据宽度大于Oracle数据表的字段宽度,怎样来增大字段的宽度呢?救命啊!(回复者有分)
- bat执行oracle文件问题
- 请教oracle导出txt文件,再用bcp导入sqlserver的问题
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)
select * from message where receivtime in (
select max(receivtime) from message group by projectId)
如果数据比较大的话可以考虑使用exist
select id,taskid ,projectida,max(date) over (partition by date) from message a;
Select *
From message
Where id In
(Select id
From (Select id, Max(receivetime) From message Group By id) A)
不好意思回答错了,这样写业务上理解错了
可以把所有字段全部加上分组函数:select max(receivtime) ,min(id),min(taskid) from message group by projectId
(select MAX(receivetime) maxdate, projectid from message
GROUP by projectid) b
where a.receivetime=b.maxdate and a.projectid=b.projectid;这个应该可以了,我自己测试了一下。
但是有一个问题,如果同一个工程里面有多条记录的receivetime都是最大的time,不知道你是只取一条记录还是都取?如果是都取的话,这个sql就没有问题了,如果只取1条,那就有问题了。
select 1 id,to_date('2009-01-01','yyyy-mm-dd') receivetime,1 taskid,'1' projectid from dual
union all
select 2 id,to_date('2009-01-02','yyyy-mm-dd') receivetime,1 taskid,'1' projectid from dual
union all
select 3 id,to_date('2009-01-01','yyyy-mm-dd') receivetime,1 taskid,'2' projectid from dual
union all
select 4 id,to_date('2009-01-03','yyyy-mm-dd') receivetime,2 taskid,'2' projectid from dual)
select id,receivetime,taskid, projectid from (
select id,receivetime,taskid, projectid,row_number() over(partition by projectid order by receivetime desc) rn from temp
) where rn = 1