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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:50:13+00:00 2026-06-04T09:50:13+00:00

I have a table that seems like this: +—–+———–+————+ | id | value |

  • 0

I have a table that seems like this:

+-----+-----------+------------+
| id  |     value |       date |
+-----+-----------+------------+
| id1 |      1499 | 2012-05-10 |
| id1 |      1509 | 2012-05-11 |
| id1 |      1511 | 2012-05-12 |
| id1 |      1515 | 2012-05-13 |
| id1 |      1522 | 2012-05-14 |
| id1 |      1525 | 2012-05-15 |
| id2 |      2222 | 2012-05-10 |
| id2 |      2223 | 2012-05-11 |
| id2 |      2238 | 2012-05-13 |
| id2 |      2330 | 2012-05-14 |
| id2 |      2340 | 2012-05-15 |
| id3 |      1001 | 2012-05-10 |
| id3 |      1020 | 2012-05-11 |
| id3 |      1089 | 2012-05-12 |
| id3 |      1107 | 2012-05-13 |
| id3 |      1234 | 2012-05-14 |
| id3 |      1556 | 2012-05-15 |
| ... |       ... |        ... |
| ... |       ... |        ... |
| ... |       ... |        ... |
+-----+-----------+------------+

What I want to do is to produce the total sum of the value column for all the data
in this table per date. There is one entry for each id per day. The problem is that
some ids haven’t a value for all days, e.g. id2 haven’t a value for the date: 2012-05-11

What I want to do is: when for a given date there is no value for a specific id, then
the value of the previous date (much closer to the given date) to be calculated in the sum.

For example, suppose we have only the data shown above. we can take the sum of all values
for a specific date from this query:

SELECT SUM(value) FROM mytable WHERE date='2012-05-12';

the result will be: 1511 + 1089 = 2600

But what I want to have is to make a query that does this calculation:
1511 + 2223 + 1089 = 4823

so that the 2223 of id2 of date 2012-05-11 is added instead of the missed value:

| id2 |  2223 | 2012-05-11 |

Do you know how can I do this through an SQL query? or through a script? e.g. python..


I have thousands of ids per date, so I would like the query to be a little bit fast if it is possible.

  • 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-04T09:50:14+00:00Added an answer on June 4, 2026 at 9:50 am

    It’s not pretty, as it has to join four copies of your table to itself, which could hit all sorts of performance pain (I strongly advise you to have indexes on id and date)… but this will do the trick:

    SELECT   y.report_date, SUM(x.value)
    FROM     mytable AS x
      NATURAL JOIN (
        SELECT   a.id, b.date AS report_date, MAX(c.date) AS date
        FROM     (SELECT DISTINCT id   FROM mytable) a JOIN
                 (SELECT DISTINCT date FROM mytable) b JOIN
                 mytable AS c ON (c.id = a.id AND c.date <= b.date)
        GROUP BY a.id, b.date
     ) AS y
    GROUP BY y.report_date
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have a table that's created like this: CREATE TABLE bin_test (id INTEGER PRIMARY
I have a table that looks like this: I have this same chart on
I have a table that looks like this:- TABLEA SERIAL SKU GRADE ID HA501
I have a table that looks like this: products -------- id, product, sku, department,
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have a table that has three different date columns, so I set each
I have a table that has records with a structure similar to this.. ID
I have a table like this with a computed column: CREATE TABLE PhoneNumbers (
I have a table that I would like to be able to present ranked
I have 3 tables that look like this: tblVideo: VideoID | Video Name 1

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.