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 8038403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:13:31+00:00 2026-06-05T03:13:31+00:00

Forgive my example if it does not make sense. I’m going to try with

  • 0

Forgive my example if it does not make sense. I’m going to try with a simplified one to encourage more participation.

Consider a table like the following:

  •        dt     |    mnth    |  foo
    --------------+------------+--------
      2012-12-01  |  December  |
        ...
      2012-08-01  |  August    |
      2012-07-01  |  July      |
      2012-06-01  |  June      |
      2012-05-01  |  May       |
      2012-04-01  |  April     |
      2012-03-01  |  March     |
        ...
      1997-01-01  |  January   |  
    

If you look for the record with dt closest to today w/o going over, what would be the best way to also return the 3 records beforehand and 7 records after?

I decided to try windowing functions:

  • WITH dates AS (
       select  row_number() over (order by dt desc)
             , dt
             , dt - now()::date as dt_diff
       from    foo
    )
    , closest_date AS (
       select * from dates
       where dt_diff = ( select max(dt_diff) from dates where dt_diff <= 0 )
    )
    
    SELECT * 
    FROM   dates
    WHERE  row_number - (select row_number from closest_date) >= -3
       AND row_number - (select row_number from closest_date) <=  7 ;
    

I feel like there must be a better way to return relative records with a window function, but it’s been some time since I’ve looked at them.

  • 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-06-05T03:13:32+00:00Added an answer on June 5, 2026 at 3:13 am
    create table foo (dt date);
    insert into foo values
    ('2012-12-01'),
    ('2012-08-01'),
    ('2012-07-01'),
    ('2012-06-01'),
    ('2012-05-01'),
    ('2012-04-01'),
    ('2012-03-01'),
    ('2012-02-01'),
    ('2012-01-01'),
    ('1997-01-01'),
    ('2012-09-01'),
    ('2012-10-01'),
    ('2012-11-01'),
    ('2013-01-01')
    ;
    
    select dt
    from (
    (
        select dt
        from foo
        where dt <= current_date
        order by dt desc
        limit 4
    )
    union all
    (
        select dt
        from foo
        where dt > current_date
        order by dt
        limit 7
    )) s
    order by dt
    ;
         dt     
    ------------
     2012-03-01
     2012-04-01
     2012-05-01
     2012-06-01
     2012-07-01
     2012-08-01
     2012-09-01
     2012-10-01
     2012-11-01
     2012-12-01
     2013-01-01
    (11 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I typed this simplified example without the benefit of an IDE so forgive any
This is driving me mad. Please forgive me but I'm not going to post
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Forgive me if this is a repeat question. I've searched StackOverflow and did not
Forgive me if this question makes little sense; I have had little sleep as
In R, one sometime sees people making references to fortunes. For example: fortune(108) What
Forgive the title, I'm not really sure how to explain what I'm seeing. Sample
Forgive my ignorance as I am not as familiar with jquery. Is there an
forgive my ignorance, I thought it was going to be a no-brainer but I
Forgive me if this has been asked before, but I assure you I've scoured

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.