0

将公司日历存储在数据库中,如下所示。请注意,公司日历从周六到周五开始:-

   Date1      Week     Month   Quarter     Year    Shift
23-FEB-13   9       6        2         2013     ABC
23-FEB-13   9       6        2         2013     DEF
22-FEB-13   9       6        2         2013     ABC1
22-FEB-13   9       6        2         2013     DEF2
21-FEB-13   8       6        2         2013     DEF2

我想做的是创建一个视图来存储 Week#、StartWeek、EndWeek,即存储当前一周的开始和结束日期,例如

Week   StartWeek    EndWeek

  9        22-FEB-13     23-FEB-13

这就是我到目前为止所拥有的

SELECT START_DATETIME, F_WEEK, ROW_NUMBER( ) OVER (PARTITION BY F_WEEK, F_YEAR ORDER BY SHIFT DESC) RNUM FROM COMMON.FISCAL_CALENDAR  WHERE F_WEEK IN (SELECT F_WEEK FROM COMMON.FISCAL_CALENDAR WHERE TO_DATE(START_DATETIME, 'DD-MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY')) AND F_YEAR IN (SELECT F_YEAR FROM COMMON.FISCAL_CALENDAR WHERE TO_DATE(START_DATETIME, 'DD-MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY')) ORDER BY START_DATETIME DESC

如果有人可以对此进行更多说明,将不胜感激?

4

3 回答 3

2

带有周开始/结束日期和周数的周年表 - 您可以使用您的日期作为开始和结束日期并添加您的计算:

SELECT start_date 
     , TRUNC(start_date, 'iw')                  wk_starts  
     , TRUNC(start_date, 'iw') + 7 - 1/86400    wk_ends
     , TO_NUMBER (TO_CHAR (start_date, 'IW'))   ISO_wk#  
 FROM
 (
  SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS start_date
    FROM dual
  CONNECT BY LEVEL <= 
  (
  SELECT TRUNC(ADD_MONTHS (SYSDATE, 12), 'Y')-TRUNC(SYSDATE, 'Y') "Num of Days in 2013"   
   FROM dual
  )
 )
/

START_DATE    WK_STARTS    WK_ENDS               ISO_WK#
----------------------------------------------------
1/1/2013    12/31/2012    1/6/2013 11:59:59 PM    1
1/2/2013    12/31/2012    1/6/2013 11:59:59 PM    1
 ....
....
1/7/2013    1/7/2013    1/13/2013 11:59:59 PM    2
1/8/2013    1/7/2013    1/13/2013 11:59:59 PM    2

这些是快速修复建议。您可以添加更多计算,因为您知道您的表格和日期...对于公司日历,您需要在上述查询中添加一些计算和更改。您可以添加结束日期。我建立了年度表,但您可以将 CONNECT BY 中的日期替换为您的日期,例如您的日期之间的天数

...CONNECT BY LEVEL <= (your_end_date - your_start_date) 

其他修复:

  1. 将 FROM 子句中的我的 start_date 替换为您的开始日期 - 这将是此更改后一年中的第一个 SAT。您可以选择任何其他 SAT 作为开始日期,不必是当年的第一个 SAT:

       SELECT Next_Day(Trunc(Sysdate,'Y'),'SAT') + LEVEL-1 AS start_date FROM dual...
    
  2. 将 CASE 添加到外部查询 - 在 ISO_Wk# 之后。删除不需要的列...:

    SELECT start_date 
     , TRUNC(start_date, 'iw')                  wk_starts  
     , TRUNC(start_date, 'iw') + 7 - 1/86400    wk_ends
     , TO_NUMBER (TO_CHAR (start_date, 'IW'))   ISO_wk# 
     , (CASE WHEN TO_CHAR(start_date, 'DY') = 'SAT' THEN start_date END) your_wk_start
     , (CASE WHEN TO_CHAR(start_date, 'DY') = 'FRI' THEN TRUNC(start_date, 'iw') + 7 - 1/86400 END) your_wk_end
      ....
    START_DATE    WK_STARTS    WK_ENDS    ISO_WK#    WK_DAY    YOUR_WK_START    YOUR_WK_END
    --------------------------------------------------------------------------------------
    1/5/2013    12/31/2012    1/6/2013 11:59:59 PM    1    SAT    1/5/2013    
    1/6/2013    12/31/2012    1/6/2013 11:59:59 PM    1    SUN        
    ....
    ....
    1/11/2013    1/7/2013    1/13/2013 11:59:59 PM    2    FRI                1/13/2013 11:59:59 PM
    1/12/2013    1/7/2013    1/13/2013 11:59:59 PM    2    SAT    1/12/2013    
    ....
    ....       
    1/18/2013    1/14/2013    1/20/2013 11:59:59 PM    3    FRI               1/20/2013 11:59:59 PM
    
于 2013-02-25T18:10:47.993 回答
1

我假设您的需求是按周分组的最小和最大日期:

SELECT Week, MIN(Date) As StartWeek, MAX(Date) As EndWeek  
FROM original_table
GROUP BY Week;

如果您只对本周感兴趣:

SELECT Week, MIN(Date) As StartWeek, MAX(Date) As EndWeek  
FROM original_table
WHERE Week = TO_CHAR(sysdate, 'IW');
于 2013-02-24T08:09:01.010 回答
0

就这个

SELECT MAX(DATE1) AS SHIFT_END_TIME, MIN(DATE1) AS SHIFT_START_TIME, WEEK, YEAR FROM MYTABLE GROUP BY WEEK, YEAR HAVING WEEK IN (SELECT WEEK FROM MYTABLE  WHERE TO_DATE(DATE1,   'DD-MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY')) AND YEAR IN (SELECT YEAR FROM MYTABLE WHERE TO_DATE(DATE1, 'DD- MON-YY') = TO_DATE(SYSDATE, 'DD-MON-YY'))
于 2013-02-24T16:32:17.200 回答