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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:35:42+00:00 2026-05-15T02:35:42+00:00

So I have a set of data that includes a column of dates and

  • 0

So I have a set of data that includes a column of dates and an ID column. Here’s a sample of the date data:

5/30/10 12:00 AM
5/30/10 12:01 AM
5/30/10 1:59 AM
5/30/10 1:59 AM
5/30/10 3:58 AM
5/30/10 3:58 AM
5/30/10 5:57 AM
5/30/10 6:57 AM
5/30/10 7:56 AM
5/30/10 7:56 AM
5/30/10 9:55 AM
5/30/10 11:54 AM

What I’d like to do is create buckets for these rows based on a parameter like “2 hours”.
These two hour windows would start at the earliest date in the dataset, but would jump to the next starting time as you scanned through the list. For example, the expected output of “buckets” for my list would be:

5/30/10 12:00 AM  1
5/30/10 12:01 AM  1
5/30/10 1:59 AM   1
5/30/10 1:59 AM   1
5/30/10 3:58 AM   2
5/30/10 3:58 AM   2
5/30/10 5:57 AM   2
5/30/10 6:57 AM   3
5/30/10 7:56 AM   3
5/30/10 7:56 AM   3
5/30/10 8:55 AM   3
5/30/10 11:54 AM  4

So you can see that when I get to 3:58 AM, it’s in the 2nd group because it’s more than 2 hours past 12:00 AM. However 5:57 AM is still in the 2nd group even though it’s more than 4 hours past 12:00 AM because the 2nd group base time is 3:58 AM, not 2:00 AM.

I’ve tried to create the grouping column by using a partition function like this:

FLOOR(DATEDIFF(SECOND, t.BaseCreateDate, t.CreateDate) / t.DedupWindow)

Where BaseCreateDate is the earliest date in my set, CreateDate is the data I listed, and DedupWindow is the 2 hours. However, that gives me fixed 2 hour windows and I can’t seem to find math that resets the base as needed through the data.

I have this working in a cursor, but for a couple of reasons I’d like to get it working set based.

  • 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-15T02:35:42+00:00Added an answer on May 15, 2026 at 2:35 am

    Edit: Here’s quite a ridiculous solution that appears to bring back the right results though.

    http://cloudexchange.cloudapp.net/stackoverflow/q/2282

     WITH DATE AS
    (
    SELECT '20100530 00:00:00.000'  AS CreateDate UNION ALL
    SELECT '20100530 00:01:00.000' UNION ALL
    SELECT '20100530 01:59:00.000' UNION ALL
    SELECT '20100530 01:59:00.000' UNION ALL
    SELECT '20100530 03:58:00.000' UNION ALL
    SELECT '20100530 03:58:00.000' UNION ALL
    SELECT '20100530 05:57:00.000' UNION ALL
    SELECT '20100530 06:57:00.000' UNION ALL
    SELECT '20100530 07:56:00.000' UNION ALL
    SELECT '20100530 07:56:00.000' UNION ALL
    SELECT '20100530 08:55:00.000' UNION ALL
    SELECT '20100530 11:54:00.000'
    ),PossibleBuckets As
    (
    SELECT     earlier.CreateDate, MIN(later.CreateDate) AS NextBucket
    FROM         Date AS earlier INNER JOIN
                          Date AS later ON later.CreateDate > earlier.CreateDate 
    and later.CreateDate > DATEADD(hour, 2, earlier.CreateDate)  
    GROUP BY earlier.CreateDate
    ),
    dates AS (
        SELECT   TOP(1)  CreateDate, NextBucket, 1 AS rank
        FROM         PossibleBuckets
        ORDER BY CreateDate
      UNION ALL
      SELECT   PB.CreateDate, PB.NextBucket, Rank + 1 AS Rank
        FROM   PossibleBuckets PB
        JOIN dates on dates.NextBucket = PB.CreateDate
    )
    SELECT d.CreateDate, COALESCE(x.Rank, (SELECT MAX(Rank)+1 FROM dates), 1) AS Rank
      FROM date d left join dates x on d.CreateDate >= x.CreateDate AND d.CreateDate < x.NextBucket
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up a Core Data model that includes an entity, Item with
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a data set that is around 700 rows with eight columns of
I have a data set that that I would like to call in a
I have a table in which one column (type ntext) has data that includes
I have some data that includes a column with delimited data. There are multiple
I have a set of data that contains garbled text fields because of encoding
I have a large set of data that is generated from a web service
I have a set of 'dynamic data' that I need to bind to the
I have a data set that resembles this: id product_id size color price created_date

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.