Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2005
    Posts
    19
    Provided Answers: 1

    Question Unanswered: Need Help Calculating Average Count by Day/Week/Month/Quarter/Year

    I need help developing a query to get the average count by the following:

    Day - use daily info for the last ??? days

    Weekly - average
    - Add all days and divide by 7
    - As of Saturday midnight

    Monthly - average
    - Add all days and divide by days in the month
    - As of last save on last day of month

    Quarter - average
    - Add all days and divide by number of days in the quarter
    - As of last day of quarter

    Year - average
    I don't have requirements for year as of yet.


    How can I get the avery count per these timeframes?

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Something like this maybe...

    Code:
    SELECT AVG(foo) OVER (PARTITION BY DATEPART(wk,bar)),
    AVG(foo) OVER (PARTITION BY DATEPART(mm,bar)),
    AVG(foo) OVER (PARTITION BY DATEPART(yyyy,bar))
    FROM blah
    “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.

  3. #3
    Join Date
    Sep 2005
    Posts
    19
    Provided Answers: 1

    Update

    I don't see the division per the requirements in the first post.

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    What do you think the average and the partition by is doing? Maybe I do not understand your requirements. Have you read Pat's sticky at the top of the board?

    Although I did get something wrong. What you are calling your weekly average sounds more like a daily average, so you might have to tweak the parameter being passed to DATEPART. As for some of your other requirements, you might to check on the value of DATEFIRST, and break it into 3 queries to handle some of the filtering.
    “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.

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Your requirements are unclear.
    "Average count" <- Que?

    How do you want your results to look?
    What have you tried so far?
    George
    Home | Blog

  6. #6
    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'

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Joe, for my benefit, can you explain the "00" convention further, please?
    I am unsure how this would apply/be used with your DDL above.
    George
    Home | Blog

  8. #8
    Join Date
    Jan 2013
    Posts
    354
    Provided Answers: 1
    MySQL started this so that the year would sort to the front of the list of dates within a year in ISO-8601 display format and like each month would sort to the front of the list of dates within a month.

    I like it because it reportng so much easier (just do a sort!), is language independent (ever see Slovak month names?) and the range look-up table is easy.


    It is not an ISO Standard yet, but I think it is under consideration.

Posting Permissions

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