Skip to content
Advertisement

How to do calculate recursive sum in SQL in configurable in Big Query

I have query problem, I’m using Google BigQuery (Just to give you context if it is different). I need two value which is Value_A and Value_B. Value_A is top X value, and Value_B is the rest of top X value.Here’s my Input Table

In this case, the value of X is 6, but I need to be configurable.

In date 20, Value_A is sum from the 6 top data in Value (which is date 14 to 20), and Value_B is sum of rest of top data (which is date 14 an below).

In date 19, Value_A is sum from the 6 top data in Value (which is date 13 to 19), and Value_B is sum of rest of top data (which is date 13 an below).

Here’s my output

Advertisement

Answer

Below is for BigQuery Standard SQL

If to apply to sample data from your question, as in below example

result is

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement