简体   繁体   English

自2000年以来的天数,周数,月数,年数,季度数

[英]Number of days, weeks,months,years,quarters since 2000

I need to populate a calendar table in a Oracle 11g database with the following: calendar_date, week_number, week_year, day_since_2000, week_since_2000, month_since_2000, quarter_since_2000, week_of_month 我需要使用以下内容填充Oracle 11g数据库中的日历表:calendar_date,week_number,week_year,day_since_2000,week_since_2000,month_since_2000,quarter_since_2000,week_of_month

The table is already populated with data since 2000 I now need to populate it going back to 1994 with negative numbers for the since_2000 columns. 自2000年以来,该表已经填充了数据,现在我要填充其数据(可追溯到1994年),因为since_2000列为负数。 I think I have everything figured but for week since 2000 and quarter since 2000. 我想除了2000年以来的一周和2000年以来的四分之一以外,我已经掌握了一切。

EDIT1: Just noticed my week since 200 and month since 2000 is still messed up. EDIT1:刚注意到我的一周(自200年以来)和2000年以来的一个月仍然混乱。 Quarter since 2000 looks good after using the answer mentioned below. 使用以下答案后,自2000年以来的季度看起来不错。 I am updating the query with the latest. 我正在使用最新的查询来更新查询。

EDIT2: It was not working because of the missing trunc(). EDIT2:由于缺少trunc(),它无法正常工作。 Its working fine now. 现在工作正常。 Latest query updated. 最新查询已更新。

This is what I am using to do that: 这就是我用来做的事情:

/* Formatted on 2/1/2013 11:54:27 AM (QP5 v5.227.12220.39724) */
CREATE OR REPLACE PROCEDURE populate_d_calendar (start_date     IN DATE,
                         end_date       IN DATE)
AS
BEGIN
   INSERT INTO d_calendar (calendar_date,
               week_number,
               week_year,
               day_since_2000,
               week_since_2000,
               month_since_2000,
               quarter_since_2000,
               week_of_month)
      SELECT dt,                                                        --Date
         TO_CHAR (dt,
              'ww'),                                --Week in the year
         TO_CHAR (dt,
              'yyyy'),                                          --Year
         TO_CHAR (dt - end_date),                         --Day Since 2000
         TRUNC (TO_CHAR (dt - end_date) / 7),            --Week Since 2000
         TRUNC (MONTHS_BETWEEN (dt,
                    end_date)),             --Month Since 2000
         CASE
        WHEN TO_CHAR (dt,
                  'MMDD') >= '0101'
         AND TO_CHAR (dt,
                  'MMDD') < '0401'
        THEN
             0
           +   4
             * TRUNC (  MONTHS_BETWEEN (TO_DATE (   '0101'
                             || TO_CHAR (dt,
                                     'yyyy'),
                             'mmddyyyy'),
                        TO_DATE ('01Jun2000'))
                  / 12)                                       --q1
        WHEN TO_CHAR (dt,
                  'MMDD') >= '0401'
         AND TO_CHAR (dt,
                  'MMDD') < '0701'
        THEN
             1
           +   4
             * TRUNC (  MONTHS_BETWEEN (TO_DATE (   '0101'
                             || TO_CHAR (dt,
                                     'yyyy'),
                             'mmddyyyy'),
                        TO_DATE ('01Jun2000'))
                  / 12)                                       --q2
        WHEN TO_CHAR (dt,
                  'MMDD') >= '0701'
         AND TO_CHAR (dt,
                  'MMDD') < '1001'
        THEN
             2
           +   4
             * TRUNC (  MONTHS_BETWEEN (TO_DATE (   '0101'
                             || TO_CHAR (dt,
                                     'yyyy'),
                             'mmddyyyy'),
                        TO_DATE ('01Jun2000'))
                  / 12)                                       --q3
        WHEN TO_CHAR (dt,
                  'MMDD') >= '1001'
         AND TO_CHAR (dt,
                  'MMDD') <= '1231'
        THEN
             3
           +   4
             * TRUNC (  MONTHS_BETWEEN (TO_DATE (   '0101'
                             || TO_CHAR (dt,
                                     'yyyy'),
                             'mmddyyyy'),
                        TO_DATE ('01Jun2000'))
                  / 12)                                       --q4
         END
        quarters_since_2000,                      --Quarter Since 2000
         TO_CHAR (dt,
              'w')                                 --Week of the month
    FROM (SELECT start_date + LEVEL - 1 dt
        FROM DUAL
          CONNECT BY LEVEL <= end_date - start_date + 1);
END;
/

i think you're after a floating point like with the others so maybe: 我认为您和其他人一样处于浮点位置,所以也许:

TO_CHAR (dt - end_date)/7 week_since_2000

and

(MONTHS_BETWEEN (dt,
            end_date)/3) quarter_since_2000

with weeks you can follow one of the methods here: 数周后,您可以按照以下方法之一进行操作:

http://searchoracle.techtarget.com/answer/Calculating-weeks-between-two-dates http://searchoracle.techtarget.com/answer/Calculating-weeks-between-two-dates

because it will depend on which rules are more useful to you 因为这取决于哪些规则对您更有用

with quarters , use this: 季度 ,使用此:

case 
         WHEN TO_CHAR (dt,'MMDD') >= '0101' and TO_CHAR (dt,'MMDD') < '0401' then  0 + 4 * trunc(months_between( to_date('0101' || to_char(dt,'yyyy'),'mmddyyyy'),to_date('01Jun2000' )) /12)   --q1
         WHEN TO_CHAR (dt,'MMDD') >= '0401' and TO_CHAR (dt,'MMDD') < '0701' then  1 + 4 * trunc(months_between( to_date('0101' || to_char(dt,'yyyy'),'mmddyyyy'),to_date('01Jun2000' )) /12)  --q2
         WHEN TO_CHAR (dt,'MMDD') >= '0701' and TO_CHAR (dt,'MMDD') < '1001' then  2 + 4 * trunc(months_between( to_date('0101' || to_char(dt,'yyyy'),'mmddyyyy'),to_date('01Jun2000' )) /12)  --q3
         WHEN TO_CHAR (dt,'MMDD') >= '1001' and TO_CHAR (dt,'MMDD') <= '1231' then 3 + 4 * trunc(months_between( to_date('0101' || to_char(dt,'yyyy'),'mmddyyyy'),to_date('01Jun2000' )) /12)   --q4
         end         quarters_since_2000,  

This is my version of calendar table from 1/1/2013 till today. 这是我从1/1/2013到今天的日历表版本。 I'm not sure how do you need to insert number of months, quarters, days etc... You need to display total number of months between every year or once? 我不确定您该如何插入月份,季度,天数等。您需要显示每年或一次之间的月份总数? You can add those calc yourself or add desired output in your post to make it clear. 您可以自己添加这些计算,也可以在帖子中添加所需的输出以使其清楚。 See addl queries/comments below: 请参阅下面的addl查询/评论:

-- Days,weeks, quarters from 1/1/2013 --
SELECT start_date                               -- 1/1/2013 --
    , TRUNC(start_date, 'iw')                  wk_starts  
    , TRUNC(start_date, 'iw') + 7 - 1/86400    wk_ends
    , TO_NUMBER (TO_CHAR (start_date, 'IW'))   ISO_wk#  
    , TO_NUMBER (TO_CHAR (start_date, 'Q'))    Quarters  
 FROM
  (
   SELECT TRUNC(SYSDATE, 'Y')-1 + LEVEL AS start_date  
     FROM dual
   CONNECT BY LEVEL <= 
  (  -- replace this part to go back to 1994 - see below --
  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#     QUARTERS
---------------------------------------------------------------------
1/1/2013    12/31/2012  1/6/2013 11:59:59 PM    1           1
1/2/2013    12/31/2012  1/6/2013 11:59:59 PM    1           1
......
2/19/2013   2/18/2013   2/24/2013 11:59:59 PM   8           1
2/20/2013   2/18/2013   2/24/2013 11:59:59 PM   8           1
......
3/10/2013   3/4/2013    3/10/2013 11:59:59 PM   10          1
3/11/2013   3/11/2013   3/17/2013 11:59:59 PM   11          1
.......

-- 6971 days from 1/1/1994 till today - 2/1/2013 --
SELECT TRUNC(SYSDATE) - TRUNC(ADD_MONTHS (SYSDATE, -12*19), 'Y') "Num of Days from 1994"
  FROM dual
/

-- 4780 days from 1/1/2000 till today - 2/1/2013 --
SELECT TRUNC(SYSDATE) - TRUNC(ADD_MONTHS (SYSDATE, -12*13), 'Y') "Num of Days from 2000"
  FROM dual
/

-- 156 Months between from 2000 toll today --
SELECT MONTHS_BETWEEN(TRUNC(SYSDATE), TRUNC(ADD_MONTHS (SYSDATE, -12*13))) mo_btw_2000_till_today 
  FROM dual
/

-- Number of weeks btw. 2 dates -- 
SELECT to_char(sysdate, 'WW') - to_char(sysdate-30, 'WW') number_of_weeks FROM dual
/

This may not be exactly what you want... To help you more I need to see the actual expected output of your query. 这可能不完全是您想要的...为了帮助您更多,我需要查看查询的实际预期输出。 Just trying to help... 只是想帮助...

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

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