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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:05:42+00:00 2026-05-20T23:05:42+00:00

This is a follow up to: TSQL Group by N Seconds . (I got

  • 0

This is a follow up to: TSQL Group by N Seconds . (I got what I asked for, but didn’t ask for the right thing)

How can I get a rolling average of 1 second groups of count(*)?

So I want to return per second counts, but I also want to be able to smooth that out over certain intervals, say 10 seconds.

So one method might be to take the average per second of every 10 seconds, can that be done in TSQL?

Ideally, the time field would be returned in Unix Time.

  • 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-20T23:05:43+00:00Added an answer on May 20, 2026 at 11:05 pm

    SQL Server is not particularly good in rolling/cumulative queries.

    You can use this:

    WITH    q (unix_ts, cnt) AS
            (
            SELECT  DATEDIFF(s, '1970-01-01', ts), COUNT(*)
            FROM    record 
            GROUP BY
                    DATEDIFF(s, '1970-01-01', ts)
            )
    SELECT  *
    FROM    q q1
    CROSS APPLY
            (
            SELECT  AVG(cnt) AS smooth_cnt
            FROM    q q2
            WHERE   q2.unix_ts BETWEEN q1.unix_ts - 5 AND q1.unix_ts + 5
            ) q2
    

    , however, this may not be very efficient, since it will count the same overlapping intervals over an over again.

    For the larger invervals, it may be even better to use a CURSOR-based solution that would allow to keep intermediate results (though normally they are worse performance-wise than pure set-based solutions).

    Oracle and PostgreSQL support this clause:

    WITH    q (unix_ts, cnt) AS
            (
            SELECT  TRUNC(ts, 'ss'), COUNT(*)
            FROM    record 
            GROUP BY
                   TRUNC(ts, 'ss')
            )
    SELECT  q.*,
            AVG(cnt) OVER (ORDER BY unix_ts RANGE BETWEEN INTERVAL '-5' SECOND AND INTERVAL '5' SECOND)
    FROM    q
    

    which keeps an internal window buffer and is very efficient.

    SQL Server, unfortunately, does not support moving windows.

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

Sidebar

Related Questions

I follow this rule but some of my colleagues disagree with it and argue
I tried to follow this but the default modelbinder let my array null on
This a follow-up to a previous question . How can I optimize this query
I follow this post What is the simplest and most robust way to get
I realize this question is very likely to have been asked before, but I've
This is follow up to: Android local variable get's lost when using camera intent
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
If you follow this link to one of the pages on my site and
I am trying to follow this tutorial on how to connect to a database
I am trying to follow this example (from p137 of Rob Pickering's Foundations of

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.