DEV Community

Cover image for Segmented Accumulation by Condition — From SQL to SPL #30
Judith-Data-Processing-Hacks
Judith-Data-Processing-Hacks

Posted on

2 1 1 1 1

Segmented Accumulation by Condition — From SQL to SPL #30

Problem description & analysis:

The ID field of a certain database table is used for sorting, the logic field is used for conditional judgment, and val is used for segmented accumulation.

source table

Task: Now we need to add a calculated column output. When logic==true, output is set to 1. Otherwise, output is accumulated and the value is the output of the previous row + val.

expected results

Code comparisons:

SQL

with table1 as (
SELECT *, countif(logic) over win1 as logic_run
FROM example_data
window win1 as (order by id rows between unbounded preceding and current row)
)
SELECT *,
sum(val) over win2 as sum_over,
sum(if(logic,1,val)) over win2 as output
from table1
window win2 as (partition by logic_run order by id rows between unbounded preceding and current row)
Enter fullscreen mode Exit fullscreen mode

SQL requires multiple window functions and subqueries to indirectly implement cumulative calculations, which is cumbersome in terms of code.

SPL: SPL provides syntax of relative positions that allows for direct accumulation.

✍🏻 Try.DEMO

esProc SPL code

A1: Load data.

A2: Add a calculated column, if logic is true, set it to 1; Otherwise, set it as the output of the previous row + val. [-1] represents the previous row.


Experience esProc SPL FREE Download — Free Trial, No Hassle!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
judith677 profile image
Judith-Data-Processing-Hacks

Please feel free to share your feedback with us and reach out if you have any data processing problems! We'd love to hear from you!!🫶🏻💡

🥳Discord
🥳Reddit

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more