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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:49:19+00:00 2026-05-28T02:49:19+00:00

i would like to find/mark every 4th day in a continuous date stream inserted

  • 0

i would like to find/mark every 4th day in a continuous date stream inserted into my table for each user in a given date range

CREATE TABLE mytable (
  id INTEGER,
  myuser INTEGER,            
  day DATE NOT NULL,
  PRIMARY KEY  (id)      
);

the problem is, that only 3 continuous days are valid per user, after that, there has to be a one day “break”

 id  | myuser |    day     |
-----+--------+------------+
  0  |    200 | 2012-01-12 | }
  1  |    200 | 2012-01-13 | }--> 3 continuous days
  2  |    200 | 2012-01-14 | }
  3  |    200 | 2012-01-15 | <-- not ok, user 200 should get warned and delete this
  4  |    200 | 2012-01-16 | }
  5  |    200 | 2012-01-17 | }--> 3 continuous days
  6  |    200 | 2012-01-18 | }
  7  |    200 | 2012-01-19 | <-- not ok, user 200 should get warned and delete this
  8  |    201 | 2012-01-12 | }
  9  |    201 | 2012-01-13 | }--> 3 continuous days
  10 |    201 | 2012-01-14 | }
  11 |    201 | 2012-01-16 | <-- ok, there is a one day gap here
  12 |    201 | 2012-01-17 | 

the main goal is to look at a given date range (usually a month) and identify days, which are not allowed. Also i have to take care that the overlapping dates are handled correctly, for example, if i look on a date range from 2012-02-01 to 2012-02-29, 2012-02-01 could be a “break” day if 2012-01-29 to 2012-01-31 is present in that table for the same user.

  • 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-28T02:49:20+00:00Added an answer on May 28, 2026 at 2:49 am

    I don’t have access to PostgreSQL, but hopefully this works…

    WITH
      grouped_data AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY myuser ORDER BY day) - (day - start_date) AS user_group_id,
        myuser,
        day
      FROM
        myTable
      WHERE
            day >= start_date - 3
        AND day <= end_date
    )
    ,
      sequenced_data AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY myuser, user_group_id ORDER BY day) AS sequence_id,
        myuser,
        day
      FROM
        grouped_data
    )
    SELECT
      myuser,
      day,
      CASE WHEN sequence_id % 4 = 0 THEN 1 ELSE 0 END as should_be_a_break_day
    FROM
      sequenced_data
    WHERE
      day >= start_date
    

    Sorry I didn’t explain the workings, I had to jump into a meeting 🙂

    Example with start_date = ‘2012-01-14’…

     id | myuser |    day     | ROW_NUMBER() | day - start_date | user_group_id
    ----+--------+------------+--------------+------------------+---------------
      0 |    200 | 2012-01-12 |     1        |        -2        |  1 - -2 = 3
      1 |    200 | 2012-01-13 |     2        |        -1        |  2 - -1 = 3
      2 |    200 | 2012-01-14 |     3        |         0        |  3 -  0 = 3
      3 |    200 | 2012-01-15 |     4        |         1        |  4 -  1 = 3
      4 |    200 | 2012-01-16 |     5        |         2        |  5 -  2 = 3
    ----+--------+------------+--------------+------------------+---------------
      5 |    201 | 2012-01-12 |     1        |        -2        |  1 - -2 = 3
      6 |    201 | 2012-01-13 |     2        |        -1        |  2 - -1 = 3
      7 |    201 | 2012-01-14 |     3        |         0        |  3 - -1 = 3
      8 |    201 | 2012-01-16 |     4        |         2        |  4 -  2 = 2
    

    Any sequential dates will have the same user_group_id. Each ‘gap’ in the days makes that user_group_id decrease by 1 (see row 8, if the record was for the 17th, a 2 day gap, the id would have been 1).

    Once you have a group_id, row_number() can be easily used to say which day in the sequence it is. A max of 3 day is the same as “Every 4th day should be a gap”, and “x % 4 = 0” identifies every 4th day.

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

Sidebar

Related Questions

I would like to find all the rows in a table and match on
I would like to find all duplicate records by name in a customer table
I would like to do something like: find . -iname *Advanced*Linux*Program* -exec kpdf {}
I would like to find out safe ways of implementing three dimensional arrays of
I would like to find out if a particular python datetime object is older
I would like to find out where I would be able to find the
I would like to find out what is the most significant differences of MS
I would like to find out, in JavaScript, which element currently has focus. I've
I would like to find a way to see what happens while my XAML
I would like to find the values of some of windows API constants, such

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.