查询根据今天的日期选择完整的季度日期

时间:2016-04-07 09:31:35

标签: sql oracle oracle11g

我写了一个查询来选择特定日期。

如果我在第一个财政星期一运行查询,即季度结束后的第一个星期一,那么它应该选择整个上个月的日期。

如果我在其他星期一运行查询,那么它应该选择当前季度的数据直到今天的日期。

以下是我尝试的查询:

SELECT 
CASE 
WHEN Q.GL_PERIOD_NUMBER IN (1,4,7,10,12) AND Q.DAY_NAME='MONDAY' AND Q.MONTHEND_FLAG='Y' THEN 'FULL_QUARTER'
WHEN Q.GL_PERIOD_NUMBER IN (2,3,5,6,8,9,11) AND Q.DAY_NAME='MONDAY' AND Q.MONTHEND_FLAG='N' THEN 'PARTIAL QUARTER'
ELSE 'NO RUN'
END RUN
FROM 
(SELECT A.GL_MONTHEND_FLAG MONTHEND_FLAG, B.DAY_NAME DAY_NAME , B.gl_period_number GL_PERIOD_NUMBER
FROM 
(SELECT TRIM(GL_MONTHEND_FLAG) GL_MONTHEND_FLAG
FROM RSE.RSE_CD_D_CALENDAR WHERE TRUNC(CALENDAR_DATE)=TRUNC(SYSDATE-12)) A,
(SELECT TRIM(DAY_NAME) DAY_NAME, gl_period_number GL_PERIOD_NUMBER
FROM RSE.RSE_CD_D_CALENDAR WHERE TRUNC(CALENDAR_DATE)=TRUNC(SYSDATE-10)) B) Q;

我已经找到了条件,但我无法设计选择日期的语法。

如果有人提示,那对我有帮助!

以下是表格结构:

desc RSE_CD_D_CALENDAR
Name                      Null Type              
------------------------- ---- ----------------- 
CALENDAR_DATE                  DATE              
DATE_REFERENCE                 VARCHAR2(10 CHAR) 
GL_DAYEND_COMPLETE_FLAG        VARCHAR2(1 CHAR)  
DAY_OF_WEEK                    NUMBER            
DAY_NAME                       VARCHAR2(15 CHAR) 
DAY_NUMBER                     NUMBER            
DW_PROCESSING_WEEK_ID          NUMBER            
DW_FISCAL_WEEK_ID              NUMBER            
CALENDAR_WEEK_NUMBER           NUMBER            
GL_WEEK_NUMBER                 NUMBER            
GL_WEEKEND_FLAG                VARCHAR2(1 CHAR)  
GL_WEEKEND_COMPLETE_FLAG       VARCHAR2(1 CHAR)  
DW_MONTH_ID                    NUMBER            
GL_PERIOD                      VARCHAR2(6 CHAR)  
GL_PERIOD_NUMBER               NUMBER            
MONTH_SHORT_NAME               VARCHAR2(3 CHAR)  
MONTH_LONG_NAME                VARCHAR2(15 CHAR) 
GL_PERIOD_KEY                  NUMBER            
GL_MONTHEND_FLAG               VARCHAR2(1 CHAR)  
GL_MONTHEND_COMPLETE_FLAG      VARCHAR2(1 CHAR)  

DW_QTR_ID                      NUMBER            
QTR_NUMBER                     NUMBER            
GL_QTR_END_FLAG                VARCHAR2(1 CHAR)  
GL_QTR_END_COMPLETE_FLAG       VARCHAR2(1 CHAR)  
GL_YEAR                        NUMBER            
GL_YEAREND_FLAG                VARCHAR2(1 CHAR)  
GL_YEAREND_COMPLETE_FLAG       VARCHAR2(1 CHAR)  
MANUAL_ADJ_COMPLETE_FLAG       VARCHAR2(1 CHAR)  
PROCESSING_WEEK_RELATIVE       NUMBER            
FISCAL_WEEK_RELATIVE           NUMBER            
MONTH_RELATIVE                 NUMBER            
QTR_RELATIVE                   NUMBER            
YEAR_RELATIVE                  NUMBER            
CALENDAR_MONTH_NUMBER          NUMBER            
CALENDAR_MONTH_SHORT_NAME      VARCHAR2(4000)    
BILLING_DAY_FLAG               VARCHAR2(1)       
QUARTER_NAME                   VARCHAR2(6)       
DW_CALENDAR_MONTH_ID           NUMBER            
CALENDAR_MONTH_RELATIVE        NUMBER            
CALCULATION_DATE               DATE              
BILLING_DAY_USA                NUMBER            
BILLING_DAY                    NUMBER            

相关字段存在于当前,0,-1,-2表示相应的前几个月等。

1 个答案:

答案 0 :(得分:2)

I think I would go with this approach:

with quarter_start_dt as (
select 
case when trunc(sysdate-7, 'Q') = trunc(sysdate, 'Q') then -- current quarter
trunc(sysdate, 'Q') 
 when trunc(sysdate-7, 'Q') <> trunc(sysdate, 'Q') then -- previous quarter
trunc(sysdate-7, 'Q')
end qs_dt
from dual)
select * from table, quarter_start_dt where
condition_dt >=  qs_dt;

additionally I guess, that for previous quarter you'd like to pick the quarter end date and use it as an additional condition to cut out anything done after the quarter end date.

EDIT: The "with" query is used to pick the boundary date - qs_dt (quarter start date). trunc(date, 'Q') returns start of the quarter date, so I'm checking if it was the same or different quarter a week ago - if it is the same quarter then use current quarter start date as boundary date, if week ago the quarter start date was different, it means it's a start of the new quarter and you want to use the start of previous quarter as the boundary date. Then take your query (which I replaced with simple select for readability) and use qs_dt as the quarter start date in the where clause.

Hope it helps.