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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:13:03+00:00 2026-05-28T16:13:03+00:00

I need to answer a question like this: For each user , what is

  • 0

I need to answer a question like this:

For each user, what is the most items that user viewed in any 60 second
time frame between START_TIMESTAMP and END_TIMESTAMP?

The 60 second time frame is a sliding window. It’s not just a matter of “items viewed” counts for each whole minute. Also, 60 seconds was just an example, it should work for any number of seconds.

My data is stored like this:

-- Timestamped log of users viewing items
CREATE TABLE user_item_views (
  user_id integer,
  item_id integer,
  timestamp timestamp
);

Doing it for each whole minute is easy enough, just format timestamp to something like YYYY-MM-DD hh:mm and do a count grouped by that formatted timestamp and the user_id.

Doing it for a sliding window, I have no idea how to approach.

If this would be easier outside of SQL, I am open to exporting the data to another format, or using another language.

Desired output is something like:

User ID  Max items viewed in N seconds, between START and END.
...      ...
...      ...
...      ...

How can I do this?

  • 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-28T16:13:05+00:00Added an answer on May 28, 2026 at 4:13 pm

    Here’s how I would do it (beware, untested code, this ist just to outline the idea).

    You need a helper table with as many rows as there are seconds between START_TIMESTAMP and END_TIMESTAMP. Create that as a temp table before you begin your query.

    For the sake of the sample, let’s call it every_second. I’m assuming your minimum time resolution is one second.

    Then do:

    SELECT
      s.timestamp,
      v.user_id,
      (
       SELECT COUNT(*) FROM user_item_views 
        WHERE timestamp BETWEEN s.timestamp AND ADDTIME(s.timestamp, '00:00:59')
              AND user_id = v.user_id
      ) item_count
    FROM
      every_second s
      LEFT JOIN user_item_views v ON v.timestamp = s.timestamp
    GROUP BY
      s.timestamp, 
      v.user_id
    

    Store that in another temporary table and select the desired maxima from it (this is necessary because of the “select max from group” problem).

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

Sidebar

Related Questions

I have a question that doesn't need any specific answer, just a general direction
I really need an answer to this question. I am working on a project
Weird question, but i need to find an answer. Is there any way, in
I am having an ASP.net page with userid and question and answer I need
I couldn't find a straight answer to my question and need to know it
I need a quick yes/no answer on this... Is it possible to get a
I need to have a single instance application (as per this answer ), but
I need to implement a view where I need the user to answer several
This is my second question about this topic, the original question can be found
I almost found the answer to this question in this thread (samplebias's answer); however

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.