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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:21:14+00:00 2026-06-06T22:21:14+00:00

I have the following query to count all data every minute. $sql= SELECT COUNT(*)

  • 0

I have the following query to count all data every minute.

$sql= "SELECT COUNT(*) AS count, date_trunc('minute', date) AS momento
FROM p WHERE fk_id_b=$id_b GROUP BY date_trunc('minute', date) 
ORDER BY momento ASC";

What I need to do is get the sum of the count for each row with the count of the 2 past minutes.

For example with the result of the $sql query above
|-------date---------|----count----|
|2012-06-21 05:20:00 |      12     |
|2012-06-21 05:21:00 |      14     |
|2012-06-21 05:22:00 |      10     |
|2012-06-21 05:23:00 |      20     |
|2012-06-21 05:24:00 |      25     |
|2012-06-21 05:25:00 |      30     |
|2012-06-21 05:26:00 |      10     |  

I want this result:

|-------date---------|----count----|
|2012-06-21 05:20:00 |      12     |   
|2012-06-21 05:21:00 |      26     |     12+14
|2012-06-21 05:22:00 |      36     |     12+14+10
|2012-06-21 05:23:00 |      44     |     14+10+20
|2012-06-21 05:24:00 |      55     |     10+20+25
|2012-06-21 05:25:00 |      75     |     20+25+30
|2012-06-21 05:26:00 |      65     |     25+30+10
  • 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-06T22:21:16+00:00Added an answer on June 6, 2026 at 10:21 pm

    This is not so tricky with lag() window function (also on SQL Fiddle):

    CREATE TABLE t ("date" timestamptz, "count" int4);
    INSERT INTO t VALUES
    ('2012-06-21 05:20:00',12),
    ('2012-06-21 05:21:00',14),
    ('2012-06-21 05:22:00',10),
    ('2012-06-21 05:23:00',20),
    ('2012-06-21 05:24:00',25),
    ('2012-06-21 05:25:00',30),
    ('2012-06-21 05:26:00',10);
    
    SELECT *,
        "count"
        + coalesce(lag("count", 1) OVER (ORDER BY "date"), 0)
        + coalesce(lag("count", 2) OVER (ORDER BY "date"), 0) AS "total"
      FROM t;
    
    1. I’ve double-quoted date and count columns, as these are reserved words;
    2. lag(field, distance) gives me the value of the field column distance rows away from the current one, thus first function gives previous row’s value and second call gives the value from the one before;
    3. coalesce() is required to avoid NULL result from lag() function (for the first row in your query there’s no “previous” one, thus it’s NULL), otherwise the total will also be NULL.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
I have following rather simple query select count(*) from tbl t1, tbl t2 For
I have the following query: SELECT DISTINCT w.name, count(*) FROM widgets AS w JOIN
Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
I'm using Oracle 10.2 and have the following query: select h.company, count(*) from history
I have the following query: SELECT location, step, COUNT(*), AVG(foo), YEAR(start), MONTH(start), DAY(start) FROM
I'm beggining with MYSQL. I have the following query: SELECT answers.data. * , COUNT(
I have the following MySQL query: SELECT (COUNT(id) * 5) AS total FROM content
I have the following MySQL query: SELECT SUM(IF(num_content>3,3,num_content)) FROM ( SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt,
I have the following query SELECT s.name, s.surname, s.id_nr, s.student_nr, s.createdate, s.enddate, (SELECT count(*)

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.