oracle数据库,SQL语句怎么写,求助

如果选择的时间间隔是:“1年“ 那个就将每年的最后一个数据显示出来
如果选择的时间间隔是:“1月“ 那个就将每月的最后一个数据显示出来
如果选择的时间间隔是:“1日“ 那个就将每天的最后一个数据显示出来
如果选择的时间间隔是:“1时“ 那个就将每小时的最后一个数据显示出来

oracle数据库,SQL语句怎么写,求助

按年等日期分组,取每组事件最大的。

思路:按时间, 唯一主键ID,分组降序排序,rn 为1的记录为时间最大的那条记录, 再通过唯一主键ID去关联表

---年
 select t2.* from
(select 
       row_number() over(partition by to_char(a.create_time,'yyyy') order by a.create_time desc) rn,
       a.tid tid 
 from table1 a 
) t1, table1 t2
where t1.rn = 1
  and t1.tid = t2.tid
    ------------------

    月
    select t2.* from
(select 
       row_number() over(partition by to_char(a.create_time,'yyyymm') order by a.create_time desc) rn,
       a.tid tid 
 from table1 a 
) t1, table1 t2
where t1.rn = 1
  and t1.tid = t2.tid


时间按格式以此类推

select * from V1;
20150106101010
20150106101110
20150106101210
20150106101310
20150116101010
20150126101110
20150136101210
20150146101310
20150116101010
20150226101110
20150336101210
20150446101310

select substr(dt,0,8) time, max(dt) dt from V1 group by 1
20150126 20150126101110
20150146 20150146101310
20150226 20150226101110
20150336 20150336101210
20150106 20150106101310
20150116 20150116101010
20150446 20150446101310
20150136 20150136101210

---年
select t2.* from
(select
row_number() over(partition by to_char(a.create_time,'yyyy') order by a.create_time desc) rn,
a.tid tid
from table1 a
) t1, table1 t2
where t1.rn = 1
and t1.tid = t2.tid
------------------

月
select t2.* from

(select
row_number() over(partition by to_char(a.create_time,'yyyymm') order by a.create_time desc) rn,
a.tid tid
from table1 a
) t1, table1 t2
where t1.rn = 1
and t1.tid = t2.tid

不对啊,数据把所有采集的数据都显示出来了,这个不知道哪里有要注意的地方,谢谢huang,我测试了,过不了