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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:29:10+00:00 2026-05-25T06:29:10+00:00

How can I compress / aggregate / group a table with events dynamically over

  • 0

How can I compress / aggregate / group a table with events dynamically over time.
I have a table with values and time of occurrence.

Something like this:

value_col   time_col
3         | 2011-02-16 22:21:05.250
2         | 2011-02-16 21:21:06.170
15        | 2011-02-16 21:21:05.250

I need to aggregate the values by a given time span (e.g. hourly) starting from the first row (latest event). So in this example I want to end up with two rows for hourly aggregation.

5
15

So if a new value comes in:

value_col   time_col
6         | 2011-02-16 23:21:05.247
3         | 2011-02-16 22:21:05.250
2         | 2011-02-16 21:21:06.170
15        | 2011-02-16 21:21:05.250

If I would run that query again I want to end up with:

9
17

It should be easy to change the time span in the query. For example compress over the last 30 seconds, past 6 hours, past 24 hours , etc.. How can I do that in oracle and MS SQL?

  • 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-25T06:29:11+00:00Added an answer on May 25, 2026 at 6:29 am

    Thanks to the previous answers I got the idea on how to fulfill all the requirements.

    For each record I calculate the time difference to the latest record in milliseconds (or seconds, depending on resolution). I then modulo the difference with the time span that I am currently interested in (e.g. 3600 sec = 1 h).
    Then I add that value to the time_col of the same record and group over that.

    Create table:

    CREATE TABLE [dbo].[test_table](
        [value_col] [int] NOT NULL,
        [time_col] [datetime] NOT NULL
    ) ON [PRIMARY]
    GO
    INSERT [dbo].[test_table] ([value_col], [time_col]) VALUES (3, CAST(0x00009E8C01705737 AS DateTime))
    INSERT [dbo].[test_table] ([value_col], [time_col]) VALUES (2, CAST(0x00009E8C015FDD8B AS DateTime))
    INSERT [dbo].[test_table] ([value_col], [time_col]) VALUES (15, CAST(0x00009E8C015FDC77 AS DateTime))
    INSERT [dbo].[test_table] ([value_col], [time_col]) VALUES (6, CAST(0x00009E8C0180D1F6 AS DateTime))
    

    Solution for SQL:

    SELECT SUM(value_col) AS s_val, aggregation_time FROM 
     (SELECT value_col, time_col, 
      DATEADD(millisecond,DATEDIFF(millisecond,time_col,(SELECT MAX(time_col) 
      FROM test_table)) % (3600 * 1000), time_col) AS aggregation_time 
     FROM test_table)
    GROUP BY aggregation_time
    ORDER BY aggregation_time DESC
    

    Solution for Oracle:

    SELECT SUM(value_col) as s_val, aggregation_time FROM
     (SELECT value_col, time_col + 
      (MOD(ROUND(((CAST((SELECT MAX(time_col) FROM test_table) AS DATE ) - 
      CAST(time_col AS DATE ))*86400),0),3600))/86400 as aggregation_time
      FROM test_table l)     
    GROUP BY aggregation_time
    ORDER BY aggregation_time DESC
    

    If I want to aggregate over the last 2 h I just change 3600 to 7200 seconds.

    The result is:

    9   2011-02-16 23:21:05.247
    17  2011-02-16 22:21:05.247
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i compress and image file(*bmp,*jpeg) in C#, I have to display some
how can I compress (minify) my JavaScript and CSS files dynamically in TYPO3? Thanks
I have found utilities that can compress html, javascript, and css files individually. However,
I look for tool which can compress JavaScript source code. I found some web
I've learned that MySQL can compress communication between servers and clients. Compression is used
Using the System.IO.Compression namespaces classes GZIPStream and DeflateStream I successfully can compress and decompress
How can you compress inline script and style tags? YSlow says In addition to
i know we can compress response by declaring Response.Filter as GZip or Delfalte streams,
I want something like a static class variable, except when different applications load my
Is there an OSS which can compress a text to a synopsis? My goal

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.