trunc()函数将日期截断为指定的时间段;所以trunc(sysdate,'mm')会返回当月的开头。然后,您可以使用add_months()函数获取上个月的开头,如下所示:
select count(distinct switch_id)
from xx_new.xx_cti_call_details@appsread.prd.com
where dealer_name = 'XXXX'
and creation_date >= add_months(trunc(sysdate,'mm'),-1)
and creation_date < trunc(sysdate, 'mm')作为一个小方面,你没有明确地转换为原始查询中的日期。始终使用date literal,例如, DATE 2012-08-31或to_date()函数,例如to_date('2012-08-31','YYYY-MM-DD')。如果你不这样做,你肯定会在某些时候弄错。
您不会使用sysdate - 15,因为这会提供当前日期之前15天的日期,这似乎不是您所追求的日期。它还包括一个时间组件,因为您没有使用trunc()。
就像trunc(,'mm')所做的一点演示一样:
select sysdate
, case when trunc(sysdate,'mm') > to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
then 1 end as gt
, case when trunc(sysdate,'mm') < to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
then 1 end as lt
, case when trunc(sysdate,'mm') = to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
then 1 end as eq
from dual
;
SYSDATE GT LT EQ
----------------- ---------- ---------- ----------
20120911 19:58:51 1