select * from tbname where (type,date) in(
select type,max(date) from tbname group by type);
select type,max(date) from tbname group by type);
解决方案 »
- Ubuntu 12.04 64位的系统安装oracle10g(64位) ,安装数据库实例时出现问题。
- triggers删除问题与partition by
- oracle 表设计方案求教
- JDBC中,获取oracle表中的主键问题
- 查询最小值的弱问题
- 简单问题
- 为什么我的oms中找不到我新建的数据库。
- 有没有Oracle 存储过程的语法来着?
- 怎以在字段name中创建唯一性约束?name不建索引
- 本地oracle, cmd命令行可以登录,可以写生sql查询,plsql无法登录,连接超时
- 安装9i问题,oracle database configuration assistant 错误!hostdef扩展名不存在
- 请教:用户SYS和SYSTEM有什么区别?
以上面的表为例,现在要同时查各个type的value累计值和最新值
即结果集如下:
type total value
--------------------
1 7 4
2 5 5
3 13 7
(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(+);
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行。
*/