전월 Oracle 날짜 함수
아래에 날짜가 하드 코딩된 부분에 대한 질문이 있습니다.제 목표는 하드코딩된 날짜를 제거하는 것입니다. 쿼리는 실행될 때 이전 달의 데이터를 가져올 것입니다.
select count(distinct switch_id)
from xx_new.xx_cti_call_details@appsread.prd.com
where dealer_name = 'XXXX'
and TRUNC(CREATION_DATE) BETWEEN '01-AUG-2012' AND '31-AUG-2012'
사용할까요?sysdate-15그것을 위한 기능?
벤의 질문을 조금 수정하는 것은,
select count(distinct switch_id)
from xx_new.xx_cti_call_details@appsread.prd.com
where dealer_name = 'XXXX'
and creation_date between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1))
함수는 지정된 기간으로 날짜를 잘라냅니다.trunc(sysdate,'mm')현재 달의 시작을 반환합니다.그런 다음 기능을 사용하여 다음과 같은 방법으로 전월의 시작을 가져올 수는 다음과 같습니다.
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 2012-08-31또는 함수, 예를 들어.to_date('2012-08-31','YYYY-MM-DD')만약 당신이 그렇게 하지 않는다면, 당신은 어느 시점에서 이것을 틀리게 될 것입니다.
사용하지 않을 것입니다.sysdate - 15이것은 현재 날짜보다 15일 전의 날짜를 제공할 것이기 때문에, 당신이 원하는 것이 아닌 것처럼 보입니다.사용하지 않는 시간 구성 요소도 포함됩니다.trunc().
단지 어떤 것에 대한 약간의 시연으로서.trunc(<date>,'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
지난달 데이터
select count(distinct switch_id)
from xx_new.xx_cti_call_details@appsread.prd.com
where dealer_name = 'XXXX'
and to_char(CREATION_DATE,'MMYYYY') = to_char(add_months(trunc(sysdate),-1),'MMYYYY');
저는 이것도 효과가 있을 것이라고 믿습니다.
select count(distinct switch_id)
from xx_new.xx_cti_call_details@appsread.prd.com
where
dealer_name = 'XXXX'
and (creation_date BETWEEN add_months(trunc(sysdate,'mm'),-1) and trunc(sysdate, 'mm'))
OP가 자신의 날짜 선택 기준을 사용한 방식인 BETHIN을 사용하는 장점이 있습니다.
Oracle sql developer에서 저와 함께 작동하고 있습니다.
SELECT add_months(trunc(sysdate,'mm'), -1),
last_day(add_months(trunc(sysdate,'mm'), -1))
FROM dual
지난 n개월 데이터 검색 중
SELECT * FROM TABLE_NAME
WHERE DATE_COLUMN BETWEEN '&STARTDATE' AND '&ENDDATE';
언급URL : https://stackoverflow.com/questions/12375888/oracle-date-function-for-the-previous-month
'programing' 카테고리의 다른 글
| R에서 가장 독특한 색상을 생성하는 방법은 무엇입니까? (0) | 2023.06.16 |
|---|---|
| 기호 배열에 문자 표기법이 있습니까? (0) | 2023.06.16 |
| iPhone의 세로에 있는 UISplitViewController에 마스터 대신 세부 VC가 표시됨 (0) | 2023.06.16 |
| iPhone Simulator - 느린 연결을 시뮬레이션하시겠습니까? (0) | 2023.06.16 |
| 매개 변수가 어떤 유형으로 예상되는지 PyCharm을 어떻게 알 수 있습니까? (0) | 2023.06.16 |