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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:19:26+00:00 2026-06-15T05:19:26+00:00

I have a huge sql table (more than 1 billion) of user transactions. I’d

  • 0

I have a huge sql table (more than 1 billion) of user transactions.
I’d like to add a binary column which represents where or not the current user_id row is 40 minutes or less than the previous one.

For instance:

user_id | date                
--------+--------------------
1       | 2011-01-01 12:15:00
1       | 2011-01-01 12:00:00
8       | 2011-01-01 15:00:00
8       | 2011-01-01 14:00:00

the result of the query would be:

user_id | date                | new
--------+---------------------+----
1       | 2011-01-01 12:15:00 | 0
1       | 2011-01-01 12:00:00 | 1
8       | 2011-01-01 15:00:00 | 1
8       | 2011-01-01 14:00:00 | 1

I’d like to avoid joining the entire table to itself
and maybe use a side table or an analytic function (over-partition).

  • 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-15T05:19:27+00:00Added an answer on June 15, 2026 at 5:19 am
    select user_id,
           date,
           case
              when date - lag(date) over (partition by user_id order by date) > interval '40' minute then 1
              else 0
           end as diff_flag
    from the_table
    order by user_id, date
    

    It assumes that date is a timestamp column despite its name.

    It’s the only way I can see. An index on (user_id, date) might speed things up – especially on 9.2 where this could qualify for an index only scan. But this is going to scan the whole table (or the maybe only the index on 9.2)

    Btw: it’s not a good idea to name a column with a reserved word (date). Additionally date is a very poor name from a documentation point of view.

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

Sidebar

Related Questions

I have a huge java codebase (more than 10,000 java classes) that makes extensive
The problem is, we have a huge number of records (more than a million)
I have a huge query which uses case/when often. Now I have this SQL
I'm using MS SQL. I have a huge table with indices to make this
I have a huge table, which has 3 columns and about 333,000 rows. The
i have a huge access table containing about 400000 rows . what sql query
i have a huge sql query that is attached to the rowsource of a
I have a huge pl/sql stored procedure, where we make some deletions as long
I have a huge problem with the REPLACE SQL function in Microsoft SQL Server
I have huge data which is static. I need to save it within the

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.