在工作中总会遇到各种各样的数据统计,统计的需求各种各样,但是一定少不了统计最近几天或者几个月的数据进行展示,下面我附上我的查询方法:
1.获取当前时间的方式统计数据,采用数据库当前时间进行差值的方式
select trunc(current_date-1) time from dual
union all
select trunc(current_date-2) time from dual
union all
select trunc(current_date-3) time from dual
union all
select trunc(current_date-4) time from dual
union all
select trunc(current_date-5) time from dual
union all
select trunc(current_date-6) time from dual
union all
select trunc(current_date-7) time from dual
union all
select trunc(current_date-8) time from dual
union all
select trunc(current_date-9) time from dual
union all
select trunc(current_date-10) time from dual;
2.使用connect by rownum层级查询生成序列,ROWNUM是一个伪列,即先查到结果集之后再加上去的一个列,它的取值从1开始排依次递增。ROWNUM其实是oracle数据库从数据文件或缓冲区中读取数据的顺序。取得第一条记录则rownum值为1。
SELECT TRUNC(SYSDATE - ROWNUM) AS time
FROM DUAL
CONNECT BY ROWNUM <= 30;