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

  • SEARCH
  • Home
  • 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 3443852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:50:31+00:00 2026-05-18T08:50:31+00:00

I have a view that averages some statistics by averaging past rows relative to

  • 0

I have a view that averages some statistics by averaging past rows relative
to the current outer row. Think of a batting average for each previous at bat for each batter. This works as I would like but, I would like more control over the old_foo.dates

The views idealized query is like this:

create view myview as
select
    avg(old_foo.stuff),
    foo.person_id,
    foo.date_ as this_date

from
    foo,
    join ( select stuff, person_id, date_ from foo) as old_foo
        on old_foo.date_ < foo.date_

group by person_id, this_date
;

But what I would really like is to be able set the minimum old_foo.date from the
view so I could be able to create arbitrary moving averages on the fly.

Such as:

select * from myview where mindate > now()::date - 10

(mindate is fictitious since I lose it with the group by)

I know I can do this with a function but I would prefer not too. Would CTE’s give me more flexibility with what I want?

edit

I can’t bring the oldate column to the top level of the view without grouping it (which is not what I want.) I want the view to be general so I could just as easily do a 10 day moving average as a 20 day one, or any date I would like. The olddates in the inner query so I have no access to it once I create a view.

  • 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-18T08:50:32+00:00Added an answer on May 18, 2026 at 8:50 am

    I figured it out :)

    create view myview as
    select
        avg(old_foo.stuff),
        foo.person_id,
        foo.date_ as this_date,
        offset
    from
        generate_series(1, 100) as offset,
        foo,
        join ( select stuff, person_id, date_ from foo) as old_foo
            on old_foo.date_ < foo.date_
            and old_foo.date_ > foo.date_ - offset
    
    group by person_id, this_date, offset
    ;
    
    
    select * from myview where offset = 10;
    

    Then offset would simulate a function parameter.

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

Sidebar

Related Questions

I have a view that I want to add some custom drawing to. I
How does your employer limit/prevent pirating of its products? Some have the view that
I have a view using a master page that contains some javascript that needs
I have a stored procedure that uses a view to pull 6 averages. The
I have a view that has a list of jobs in it, with data
I have a View that can vary significantly, depending on the 'mode' a particular
I have a view that is joining two tables and ordering by the first
Image i have a view that is cached with the OutputCache attribute but i
I have a list view that is periodically updated (every 60 seconds). It was
I have a view user control that can post form. This control can be

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.