Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    188

    Unanswered: Created Calendar == Now What?

    I (found a script) and created a calander showing all dates by week for 2013 & 2014 with a StartDate & EndDate. How can I now use this calander to show weekly production in a select query?

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Vague code for a vague question
    Code:
    SELECT calendar.lower_date As week_commencing_monday
         , ...
         , ...
    FROM   (
            SELECT the_date As lower_date
                 , DateAdd(dd, 7, the_date) As upper_date
            FROM   dbo.calendar
            WHERE  is_monday = 1
           ) As calendar
     LEFT
      JOIN your_table
        ON your_table.date_field >= calendar.lower_date
       AND your_table.date_field <  calendar.upper_date
    http://gvee.co.uk/files/sql/dbo.numb...o.calendar.sql
    George
    Home | Blog

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    7:17 on a Friday Night and someone your age should be at the pub with the ladies and not answering message board posts.

    I think his question has to do with tie his front end wizardry with the backend drudgery.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #4
    Join Date
    Feb 2012
    Posts
    188
    Quote Originally Posted by gvee View Post
    Vague code for a vague question
    Code:
    SELECT calendar.lower_date As week_commencing_monday
         , ...
         , ...
    FROM   (
            SELECT the_date As lower_date
                 , DateAdd(dd, 7, the_date) As upper_date
            FROM   dbo.calendar
            WHERE  is_monday = 1
           ) As calendar
     LEFT
      JOIN your_table
        ON your_table.date_field >= calendar.lower_date
       AND your_table.date_field <  calendar.upper_date
    http://gvee.co.uk/files/sql/dbo.numb...o.calendar.sql
    Ya made it look so simple. Thank you!

  5. #5
    Join Date
    Jan 2013
    Posts
    354
    Provided Answers: 1
    Report Period Table

    Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML. The report period table gives a name to a range of dates that is common to the entire enterprise.

    CREATE TABLE Something_Report_Periods
    (something_report_name CHAR(10) NOT NULL PRIMARY KEY
    CHECK (something_report_name LIKE <pattern>),
    something_report_start_date DATE NOT NULL,
    something_report_end_date DATE NOT NULL,
    CONSTRAINT date_ordering
    CHECK (something_report_start_date <= something_report_end_date),
    etc);

    These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601 data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'

    Weeks within Year:

    I would build a calendar table with a ISO-8601 week-within-year format column in addition to the usual Common Era date. This format is 'yyyyWww-d' where yyyy is the year, W is a separator token, ww is (01-53) week number and d is (1-7) day of the week.

    You input any calendar date, find the week-within-year column and return the dates that match on a LIKE predicate.
    WHERE sale_day LIKE '2012W26-[67]'

    There are several websites with calendars you can cut & paste, but you can start your search with: Week number

  6. #6
    Join Date
    Feb 2012
    Posts
    188
    Quote Originally Posted by Celko View Post
    Report Period Table

    Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML. The report period table gives a name to a range of dates that is common to the entire enterprise.

    CREATE TABLE Something_Report_Periods
    (something_report_name CHAR(10) NOT NULL PRIMARY KEY
    CHECK (something_report_name LIKE <pattern>),
    something_report_start_date DATE NOT NULL,
    something_report_end_date DATE NOT NULL,
    CONSTRAINT date_ordering
    CHECK (something_report_start_date <= something_report_end_date),
    etc);

    These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601 data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'

    Weeks within Year:

    I would build a calendar table with a ISO-8601 week-within-year format column in addition to the usual Common Era date. This format is 'yyyyWww-d' where yyyy is the year, W is a separator token, ww is (01-53) week number and d is (1-7) day of the week.

    You input any calendar date, find the week-within-year column and return the dates that match on a LIKE predicate.
    WHERE sale_day LIKE '2012W26-[67]'

    There are several websites with calendars you can cut & paste, but you can start your search with: Week number
    This has been very informative and helpful. The issues I still face are how do you display data for weeks overlapping years, and what if your week does not run Sunday - Saturday?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •