select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒
按时间段统计:注意,要根据Oracle中实际的类型而对sql语句做相应的改变:
代码来源:
select to_char((trunc(sysdate)+trunc((dtime-trunc(sysdate))*24*60/30)*30/60/24),'hh24:mi')||'-'||to_char((trunc(sysdate)+trunc((dtime-trunc(sysdate))*24*60/30+1)*30/60/24),'hh24:mi') period, avg(v1),sum(v2),count(*) "ROWS" from test where trunc(dtime)=to_date(&day,'yyyymmdd') group by to_char((trunc(sysdate)+trunc((dtime-trunc(sysdate))*24*60/30)*30/60/24),'hh24:mi')||'-'||to_char((trunc(sysdate)+trunc((dtime-trunc(sysdate))*24*60/30+1)*30/60/24),'hh24:mi') order by 1;
我的修改:
select to_char((trunc(sysdate) +
trunc((to_date(to_char(passvehicleinfo.passtime,
'yyyymmdd hh24:mi:ss'),
'yyyymmdd hh24:mi:ss') - trunc(sysdate)) * 24 * 60 / 30) * 30 / 60 / 24),
'hh24:mi') || '-' || to_char((trunc(sysdate) + trunc((to_date(to_char(passvehicleinfo.passtime, 'yyyymmdd hh24:mi:ss'), 'yyyymmdd hh24:mi:ss') - trunc(sysdate)) * 24 * 60 / 30 + 1) * 30 / 60 / 24), 'hh24:mi')
from passvehicleinfo
group by to_char((trunc(sysdate) +
trunc((to_date(to_char(passvehicleinfo.passtime,
'yyyymmdd hh24:mi:ss'),
'yyyymmdd hh24:mi:ss') - trunc(sysdate)) * 24 * 60 / 30) * 30 / 60 / 24),
'hh24:mi') || '-' || to_char((trunc(sysdate) + trunc((to_date(to_char(passvehicleinfo.passtime, 'yyyymmdd hh24:mi:ss'), 'yyyymmdd hh24:mi:ss') - trunc(sysdate)) * 24 * 60 / 30 + 1) * 30 / 60 / 24), 'hh24:mi')