ORACLE 按时间统计数据

根据需要按时间统计数据

1、按名称统计2022-01-26到2022-04-03

select t.COLLECT_SITE_NAME,count(1) from t_cj02 t
where to_char(t.testing_datetime,'yyyy-mm-dd') between '2022-01-26' and '2022-04-03'
group by t.COLLECT_SITE_NAME
select t.COLLECT_SITE_NAME,count(1) from t_cj02 t
where to_char(t.testing_datetime,'yyyy-mm-dd') between '2022-01-26' and '2022-04-03'
group by t.COLLECT_SITE_NAME
select t.COLLECT_SITE_NAME,count(1) from t_cj02 t where to_char(t.testing_datetime,'yyyy-mm-dd') between '2022-01-26' and '2022-04-03' group by t.COLLECT_SITE_NAME

2、统计从某个时间至今的数据

select t.COLLECT_SITE_NAME,count(1) from t_cj02 t
where to_char(t.testing_datetime,'yyyy-mm-dd') >'2022-03-30'
group by t.COLLECT_SITE_NAME
select t.COLLECT_SITE_NAME,count(1) from t_cj02 t
where to_char(t.testing_datetime,'yyyy-mm-dd') >'2022-03-30'
group by t.COLLECT_SITE_NAME
select t.COLLECT_SITE_NAME,count(1) from t_cj02 t where to_char(t.testing_datetime,'yyyy-mm-dd') >'2022-03-30' group by t.COLLECT_SITE_NAME

to_date方式:

select * from tablename where time>= to_date('2011-05-02','yyyy-mm-dd')
and time<=to_date('2011-05-30','yyyy-mm-dd')
select * from tablename where time>=  to_date('2011-05-02','yyyy-mm-dd')

and  time<=to_date('2011-05-30','yyyy-mm-dd')
select * from tablename where time>= to_date('2011-05-02','yyyy-mm-dd') and time<=to_date('2011-05-30','yyyy-mm-dd')

运行的结果是:可以显示05-02的数据,但是不能显示05-30的数据。

所有可以得出结论:

①如果想显示05-30的数据可以
②如果想要显示05-30的数据可以<=to_date('2011-05-30 23:59:59 999','yyyy-mm-dd hh24:mi:ss')也是可以查出来的。

to_char方式:

同样查询上面两个日期

select * from tablename where to_char(time,'yyyy-mm-dd')>='2011-05-02'
and to_char(time,'yyyy-mm-dd')<='2011-05-30'
select * from tablename where to_char(time,'yyyy-mm-dd')>='2011-05-02'

and to_char(time,'yyyy-mm-dd')<='2011-05-30'
select * from tablename where to_char(time,'yyyy-mm-dd')>='2011-05-02' and to_char(time,'yyyy-mm-dd')<='2011-05-30'

查询结果:可以同时显示05-02和05-30的数据。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容