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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:10:14+00:00 2026-05-26T21:10:14+00:00

I need to query for each minute the total count of rows up to

  • 0

I need to query for each minute the total count of rows up to that minute.

The best I could achieve so far doesn’t do the trick. It returns count per minute, not the total count up to each minute:

SELECT COUNT(id) AS count
     , EXTRACT(hour from "when") AS hour
     , EXTRACT(minute from "when") AS minute
  FROM mytable
 GROUP BY hour, minute
  • 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-26T21:10:14+00:00Added an answer on May 26, 2026 at 9:10 pm

    Return only minutes with activity

    Shortest

    SELECT DISTINCT
           date_trunc('minute', "when") AS minute
         , count(*) OVER (ORDER BY date_trunc('minute', "when")) AS running_ct
    FROM   mytable
    ORDER  BY 1;
    

    Use date_trunc(), it returns exactly what you need.

    Don’t include id in the query, since you want to GROUP BY minute slices.

    count() is typically used as plain aggregate function. Appending an OVER clause makes it a window function. Omit PARTITION BY in the window definition – you want a running count over all rows. By default, that counts from the first row to the last peer of the current row as defined by ORDER BY. The manual:

    The default framing option is RANGE UNBOUNDED PRECEDING, which is the
    same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY,
    this sets the frame to be all rows from the partition start up
    through the current row’s last ORDER BY peer.

    And that happens to be exactly what you need.

    Use count(*) rather than count(id). It better fits your question ("count of rows"). It is generally slightly faster than count(id). And, while we might assume that id is NOT NULL, it has not been specified in the question, so count(id) is wrong, strictly speaking, because NULL values are not counted with count(id).

    You can’t GROUP BY minute slices at the same query level. Aggregate functions are applied before window functions, the window function count(*) would only see 1 row per minute this way.
    You can, however, SELECT DISTINCT, because DISTINCT is applied after window functions.

    ORDER BY 1 is just shorthand for ORDER BY date_trunc('minute', "when") here.
    1 is a positional reference reference to the 1st expression in the SELECT list.

    Use to_char() if you need to format the result. Like:

    SELECT DISTINCT
           to_char(date_trunc('minute', "when"), 'DD.MM.YYYY HH24:MI') AS minute
         , count(*) OVER (ORDER BY date_trunc('minute', "when")) AS running_ct
    FROM   mytable
    ORDER  BY date_trunc('minute', "when");
    

    Fastest

    SELECT minute, sum(minute_ct) OVER (ORDER BY minute) AS running_ct
    FROM  (
       SELECT date_trunc('minute', "when") AS minute
            , count(*) AS minute_ct
       FROM   tbl
       GROUP  BY 1
       ) sub
    ORDER  BY 1;
    

    Much like the above, but:

    I use a subquery to aggregate and count rows per minute. This way we get 1 row per minute without DISTINCT in the outer SELECT.

    Use sum() as window aggregate function now to add up the counts from the subquery.

    I found this to be substantially faster with many rows per minute.

    Include minutes without activity

    Shortest

    @GabiMe asked in a comment how to get eone row for every minute in the time frame, including those where no event occured (no row in base table):

    SELECT DISTINCT
           minute, count(c.minute) OVER (ORDER BY minute) AS running_ct
    FROM  (
       SELECT generate_series(date_trunc('minute', min("when"))
                            ,                      max("when")
                            , interval '1 min')
       FROM   tbl
       ) m(minute)
    LEFT   JOIN (SELECT date_trunc('minute', "when") FROM tbl) c(minute) USING (minute)
    ORDER  BY 1;
    

    Generate a row for every minute in the time frame between the first and the last event with generate_series() – here directly based on aggregated values from the subquery.

    LEFT JOIN to all timestamps truncated to the minute and count. NULL values (where no row exists) do not add to the running count.

    Fastest

    With CTE:

    WITH cte AS (
       SELECT date_trunc('minute', "when") AS minute, count(*) AS minute_ct
       FROM   tbl
       GROUP  BY 1
       ) 
    SELECT m.minute
         , COALESCE(sum(cte.minute_ct) OVER (ORDER BY m.minute), 0) AS running_ct
    FROM  (
       SELECT generate_series(min(minute), max(minute), interval '1 min')
       FROM   cte
       ) m(minute)
    LEFT   JOIN cte USING (minute)
    ORDER  BY 1;
    

    Again, aggregate and count rows per minute in the first step, it omits the need for later DISTINCT.

    Different from count(), sum() can return NULL. Default to 0 with COALESCE.

    With many rows and an index on "when" this version with a subquery was fastest among a couple of variants I tested with Postgres 9.1 – 9.4:

    SELECT m.minute
         , COALESCE(sum(c.minute_ct) OVER (ORDER BY m.minute), 0) AS running_ct
    FROM  (
       SELECT generate_series(date_trunc('minute', min("when"))
                            ,                      max("when")
                            , interval '1 min')
       FROM   tbl
       ) m(minute)
    LEFT   JOIN (
       SELECT date_trunc('minute', "when") AS minute
            , count(*) AS minute_ct
       FROM   tbl
       GROUP  BY 1
       ) c USING (minute)
    ORDER  BY 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a query that will return a table where each column is the
I need some help a query. For each movie in my database that has
I need to a scroll paging with jqgrid where I query the database each
Need to query a database for 12 million rows, process this data and then
I have a Delete Query that each day needs to run deleting any data
I am running a select statement in a report that returns 30 rows in
I need to query a large number of rows (containing a timestamp column) and
This query returns me the list of room #077 that is occupied on specific
I need query to get data between two days.I tried with this.But it gives
Need to query my SQL server from Access using an ADO connection (for example),

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.