Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7432973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:32:32+00:00 2026-05-29T09:32:32+00:00

Writing a SQL query that should return the average charges for a preceding 91

  • 0

Writing a SQL query that should return the average charges for a preceding 91 day window. I truncate the date down to the first day of the week and expect the 91 day average to be the 91 days prior to this date.

However when validating it appears that the 91 day average is including the current week that I truncated.

Query:

SELECT
    week AS WEEK,
    to_char(week,'ww-yyyy') AS Week_num
    --Sum of the charges for the previous 91 days / 91 will give you avg charges per day for the last 91 days
    --Need to count days regardless of if there are any charges on that day
    ,(SUM(CHARGES) OVER (ORDER BY WEEK RANGE INTERVAL '91' DAY PRECEDING))/91 MV_91_DAY_AVG
FROM 
( 

    SELECT
         --Truncate date down to first of week. Goal is to make 91 day preceding being at this date.
         TRUNC(TRANS.ORIG_POST_DATE,'WW') AS WEEK
         ,SUM(TRANS.AMOUNT) AS CHARGES
    FROM TRANS
    WHERE
        TRANS.DETAIL = "Charge"
    GROUP BY TRUNC(TRANS.ORIG_POST_DATE,'WW')
)
ORDER BY WEEK

Current output:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:32:33+00:00Added an answer on May 29, 2026 at 9:32 am

    I think you want your window clause to be:

    (ORDER BY WEEK RANGE BETWEEN INTERVAL '91' DAY PRECEDING AND INTERVAL '1' DAY PRECEDING)
    

    in order to exclude the current week’s charges.

    You may also want to change the ’91’ to ’92’ to include an additional week at the beginning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a PHP script that builds an SQL query by concatenating the string
Writing my first SQL query to run specifically as a SQL Job and I'm
I am writing a SQL query that I would like to add a column
I need help writing a T-SQL query that will generate 52 rows of data
I am facing a problem for writing an sql query which should be easy
I'm writing a SQL query in SQL Server in which I need to replace
I'm writing an SQL Query, where a few of the columns returned need to
I am writing an SQL query which involves finding if timestamp falls in particular
I am writing a query in SQL server2005. This is returning me a duplicate
I am writing some new SQL queries and want to check the query plans

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.