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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:56:12+00:00 2026-06-18T10:56:12+00:00

Any idea how to calculate running total in BigQuery SQL? id value running total

  • 0

Any idea how to calculate running total in BigQuery SQL?

id   value   running total
--   -----   -------------
1    1       1
2    2       3
3    4       7
4    7       14
5    9       23
6    12      35
7    13      48
8    16      64
9    22      86
10   42      128
11   57      185
12   58      243
13   59      302
14   60      362 

Not a problem for traditional SQL servers using either correlated scalar query:

SELECT a.id, a.value, (SELECT SUM(b.value)
                       FROM RunTotalTestData b
                       WHERE b.id <= a.id)
FROM   RunTotalTestData a
ORDER BY a.id;

or join:

SELECT a.id, a.value, SUM(b.Value)
FROM   RunTotalTestData a,
       RunTotalTestData b
WHERE b.id <= a.id
GROUP BY a.id, a.value
ORDER BY a.id;

But I couldn’t find a way to make it work in BigQuery…

  • 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-18T10:56:13+00:00Added an answer on June 18, 2026 at 10:56 am

    You probably figured it out already. But here is one, not the most efficient, way:

    JOIN can only be done using equality comparisons i.e. b.id <= a.id cannot be used.

    https://developers.google.com/bigquery/docs/query-reference#joins

    This is pretty lame if you ask me. But there is one work around. Just use equality comparison on some dummy value to get the cartesian product and then use WHERE for <=. This is crazily suboptimal. But if your tables are small this is going to work.

    SELECT a.id, SUM(a.value) as rt 
    FROM RunTotalTestData a 
    JOIN RunTotalTestData b ON a.dummy = b.dummy 
    WHERE b.id <= a.id 
    GROUP BY a.id 
    ORDER BY rt
    

    You can manually constrain the time as well:

    SELECT a.id, SUM(a.value) as rt 
    FROM (
        SELECT id, timestamp RunTotalTestData 
        WHERE timestamp >= foo 
        AND timestamp < bar
    ) AS a 
    JOIN (
        SELECT id, timestamp, value RunTotalTestData 
        WHERE timestamp >= foo AND timestamp < bar
    ) b ON a.dummy = b.dummy 
    WHERE b.id <= a.id 
    GROUP BY a.id 
    ORDER BY rt
    

    Update:

    You don’t need a special property. You can just use

    SELECT 1 AS one
    

    and join on that.

    As billing goes the join table counts in the processing.

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

Sidebar

Related Questions

Any idea why I see in the manage applications Running tab, both my application
Any idea why the following code is not working mysql credentials are correct, have
Possible Duplicate: Calculate a Running Total in SqlServer Consider this data Day | OrderCount
Any idea how to build datediff query that calculate time that possibly crosses midnight?
Can anyone give any idea to calculate the angle by which a compass needle
How I can calculate packet loss from Flex . Any Idea? Thanks in advance.
Any idea on how can I track users bookmarking event? I want to find
Any idea why I can't see H1? <div id=getTheProducts style=clear: both; margin-top: 15px; margin-bottom:
Any idea why this code: extern C __declspec(dllexport) void Transform(double x[], double y[], int
Any idea how to join multiple tables in ssis programatically using Merge Join ?

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.