请原谅我发布类似的问题。请考虑一下:
date value
18/5/2010, 1 pm 40
18/5/2010, 2 pm 20
18/5/2010, 3 pm 60
18/5/2010, 4 pm 30
18/5/2010, 5 pm 60
18/5/2010, 6 pm 25
19/5/2010, 6 pm 300
19/5/2010, 6 pm 450
19/5/2010, 6 pm 375
20/5/2010, 6 pm 250
20/5/2010, 6 pm 310
查询是获取每天的日期和值,以使当天获得的值为max。如果在该日重复最大值,则选择最低时间戳。结果应该是:
18/5/2010, 3 pm 60
19/5/2010, 6 pm 450
20/5/2010, 6 pm 310
查询应该采用类似下面给出的日期范围,并以上述方式查找该范围的结果:
,其中 date> = to_date('26 / 03/2010','DD / MM / YYYY')AND 日期< TO_DATE('27 / 03 / 2010' , 'DD / MM / YYYY')
答案 0 :(得分:2)
如果您提供CREATE TABLE和INSERT,则可以更轻松地提供经过测试的答案。
create table i (i_dt date, i_val number);
insert into i values (to_date('18/5/2010 1pm','dd/mm/yyyy hham'), 40);
insert into i values (to_date('18/5/2010 2pm','dd/mm/yyyy hham'), 20);
insert into i values (to_date('18/5/2010 3pm','dd/mm/yyyy hham'), 60);
insert into i values (to_date('18/5/2010 4pm','dd/mm/yyyy hham'), 30);
insert into i values (to_date('18/5/2010 5pm','dd/mm/yyyy hham'), 60);
insert into i values (to_date('18/5/2010 6pm','dd/mm/yyyy hham'), 25 );
insert into i values (to_date('19/5/2010 6pm','dd/mm/yyyy hham'), 300 );
insert into i values (to_date('19/5/2010 6pm','dd/mm/yyyy hham'), 450 );
insert into i values (to_date('19/5/2010 6pm','dd/mm/yyyy hham'), 375 );
insert into i values (to_date('20/5/2010 6pm','dd/mm/yyyy hham'), 250 );
insert into i values (to_date('20/5/2010 6pm','dd/mm/yyyy hham'), 310 );
select i_dt, i_val from
(select i.*, rank() over (partition by trunc(i_dt) order by i_val desc, i_dt asc) rn
from i)
where rn = 1;
答案 1 :(得分:2)
您正在聚合数据,因此请使用分组和聚合功能。您可以添加任何您想要的where子句,但我复制了您的where子句,更改日期以便选择每个记录。借用Gary的创建表和插入语句:
SQL> select min(i_dt) keep (dense_rank last order by i_val) i_dt
2 , max(i_val) i_val
3 from i
4 where i_dt >= to_date('26/03/2010','dd/mm/yyyy')
5 and i_dt < to_date('27/05/2010','dd/mm/yyyy')
6 group by trunc(i_dt)
7 /
I_DT I_VAL
------------------- ----------
18-05-2010 15:00:00 60
19-05-2010 18:00:00 450
20-05-2010 18:00:00 310
3 rows selected.
此致 罗布。
答案 2 :(得分:0)
我没试过这个,但我认为你想要的东西是:
select max(date)
from table
where date >= to_date('26/03/2010','DD/MM/YYYY') AND date < to_date('27/03/2010','DD/MM/YYYY')
group by trunc(date)