ORACLE中查询附近一个月或几天的数据

本文介绍了如何在Oracle数据库中使用SQL查询来统计最近几天或几个月的数据,包括利用current_date减去不同偏移量生成序列以及通过connectbyrownum实现层级查询的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在工作中总会遇到各种各样的数据统计,统计的需求各种各样,但是一定少不了统计最近几天或者几个月的数据进行展示,下面我附上我的查询方法:


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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值