简体   繁体   English

我想每月生成一个报告

[英]I want to generate a report monthly

--query-- -查询-

select count(*)
    from TOKEN
    where CODE = xxx
      and createdDatetime >=trunc(sysdate);

--Result-- - 结果 -

Count(*)
72

Currently I am using a monitoring tool that automatically run every day and gets the total count for whole day. 目前,我正在使用一个监视工具,该工具每天自动运行并获得一整天的总数。 Now I want to run a query that will automatically count the data for the whole month without changing the query every month. 现在,我想运行一个查询,该查询将自动计算整个月的数据,而无需每月更改查询。

A condition you use: 您使用的条件:

and createdDatetime >= trunc(sysdate);

returns data for today , as TRUNC , applied to SYSDATE , gives "very first moments of today". 返回今天的数据,因为TRUNC应用于SYSDATE ,给出“今天非常重要的时刻”。

But, if you adjust it a little bit and truncate SYSDATE to a month, you'd get the first day of current month, eg 但是,如果您稍稍调整一下并将SYSDATE截断为一个月,则会得到当前月份的第一天,例如

SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';

Session altered.

SQL> select
  2    trunc(sysdate) today,
  3    trunc(sysdate, 'mm') this_month
  4  from dual;

TODAY               THIS_MONTH
------------------- -------------------
23.07.2018 00:00:00 01.07.2018 00:00:00

SQL>

so you'd, finally, use 所以你最后会用

and createdDatetime >= trunc(sysdate, 'mm')

Depends on what you prefer by telling "the whole month" ; 通过说“整个月”来取决于您的喜好;

The period past since one-month before : 从一个月前开始过去的时间:

 select count(1)
   from TOKEN
  where CODE = 'xxx'
    and months_between(trunc(sysdate),createdDatetime)<=1;

From the beginning of the current month : 从本月初开始:

 select count(1)
   from TOKEN
  where CODE = 'xxx'
    and to_char(createdDatetime,'yyyymm') = to_char(trunc(sysdate),'yyyymm');

SQL Fiddle Demo SQL小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM