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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:42:26+00:00 2026-06-01T05:42:26+00:00

I have an activity table with a structure like this: id prd_id act_dt grp

  • 0

I have an activity table with a structure like this:

id  prd_id  act_dt               grp
------------------------------------
1   1       2000-01-01 00:00:00
2   1       2000-01-01 00:00:01
3   1       2000-01-01 00:00:02
4   2       2000-01-01 00:00:00
5   2       2000-01-01 00:00:01
6   2       2000-01-01 01:00:00
7   2       2000-01-01 01:00:01
8   3       2000-01-01 00:00:00
9   3       2000-01-01 00:00:01
10  3       2000-01-01 02:00:00

I want to split the data within this activity table by product (prd_id) and activity date (act_dt), and update the the group (grp) column with a value from a sequence for each of these groups.

The kicker is, I need to group by similar timestamps, where similar means “all records have a difference of exactly 1 second.” In other words, within a group, the difference between any 2 records when sorted by date will be exactly 1 second, and the difference between the first and last records can be any amount of time, so long as all the intermediary records are 1 second apart.

For the example data, the groups would be:

id  prd_id  act_dt               grp
------------------------------------
1   1       2000-01-01 00:00:00  1
2   1       2000-01-01 00:00:01  1
3   1       2000-01-01 00:00:02  1
4   2       2000-01-01 00:00:00  2
5   2       2000-01-01 00:00:01  2
6   2       2000-01-01 01:00:00  3
7   2       2000-01-01 01:00:01  3
8   3       2000-01-01 00:00:00  4
9   3       2000-01-01 00:00:01  4
10  3       2000-01-01 02:00:00  5

What method would I use to accomplish this?

The size of the table is ~20 million rows, if that affects the method used to solve the problem.

  • 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-01T05:42:28+00:00Added an answer on June 1, 2026 at 5:42 am

    I’m not an Oracle wiz, so I’m guessing at the best option for one line:

        (CAST('2010-01-01' AS DATETIME) - act_dt) * 24 * 60 * 60      AS time_id,
    

    This just needs to be “the number of seconds from [aDateConstant] to act_dt”. The result can be negative. It just needs to be a the number of seconds, to turn your act_dt into an INT. The rest should work fine.

    WITH
      sequenced_data
    AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY prd_id  ORDER BY act_dt)      AS sequence_id,
        (CAST('2010-01-01' AS DATETIME) - act_dt) * 24 * 60 * 60      AS time_id,
        *
      FROM
        yourTable
    )
    SELECT
      DENSE_RANK() OVER (PARTITION BY prd_id ORDER BY time_id - sequence_id) AS group_id,
      *
    FROM
      sequenced_data 
    

    Example data:

     sequence_id | time_id | t-s | group_id
    -------------+---------+-----+----------
          1      |   1     |  0  |    1
          2      |   2     |  0  |    1
          3      |   3     |  0  |    1
          4      |   8     |  4  |    2
          5      |   9     |  4  |    2
          6      |   12    |  6  |    3
          7      |   14    |  7  |    4
          8      |   15    |  7  |    4
    

    NOTE: This does assume there are not multiple records with the same time. If there are, they would need to be filtered out first. Probably just using a GROUP BY in a preceding CTE.

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

Sidebar

Related Questions

I have an IPAddress column on my Activity table. This is stored as a
I have 3 tables structured like so: activity table: activity_id, user_id, type, date reviews
I have an application that records activity in a table (Oracle 10g). The logging
I have an activity that launches another activity with startActivityForResult method. I would like
I have a table named activity with 2 columns: when as datetime // last
I have two DB tables, ACTIVITY and USER . In the ACTIVITY table I
I have two tables, arrc_PurchActivity and arrc_Voucher. The purchase activity table contains multiple records
I have three table in the Database - Activity table with activity_id, activity_type Category
In my database I have a table 'favorites' and a table 'activity'. I am
Say a user activity table. At a minimum you would need stuff like user_id,

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.