DEV Community

Cover image for Number Non-Null Values in Order within the Group — From SQL to SPL #17
Judith-Data-Processing-Hacks
Judith-Data-Processing-Hacks

Posted on

2 1 1 1 1

Number Non-Null Values in Order within the Group — From SQL to SPL #17

Problem description & analysis:

After sorting a table in the Oracle database according to the first and second columns, the third column has non-null values.

source table
Task: Now we need to add a calculated column RN_C1: within a group of data with the same CP, if DOPIS_C has consecutive non null values, then start from 1 and fill in the numbers in order; If DOPIS_C is null, fill in the null value in RN_C1 and renumber from the next nonnull value.

expected results

Code comparisons:

When using relative position to reference set members in SQL, it is necessary to use tedious window functions, often accompanied by nested subqueries; The logical judgment of the subsequent numbering calculation is also quite complex, with multiple layers of case when, and the code is very cumbersome. The sequence numbers need to be accumulated within a specified interval, which requires assembling these intervals into some kind of grouping, and the idea is very convoluted.

Of course, it is also possible to reference set members at relative positions in a loop and make logical judgments to avoid “detours”, but a single statement SQL cannot write a loop structure, which can be implemented using stored procedures. However, the code is still cumbersome and the framework is also complex.

SPL solution: SPL supports a complete procedural syntax that can handle complex business logic in loops, and can also conveniently reference set members in relative positions to simplify code.

SPL code
A1: Load data through JDBC, sort by CP and ROK, and add a null calculated column RN_C1.

A2: Modify RN_C1 for each record: When DOPIS_C is not null, if the CP of the current record is the same as the CP of the previous record, that is, within the same group, return t+1; If cross over group, set t to 1 and return. If DOPIS_C is null, set t to null and return. Note that the initial default value of variable t is null.


esProc SPL is FREE to download and give it a try: esProc SPL FREE Download.

Image of Datadog

Optimize UX with Real User Monitoring

Learn how Real User Monitoring (RUM) and Synthetic Testing provide full visibility into web and mobile performance. See best practices in action and discover why Datadog was named a Leader in the 2024 Gartner MQ for Digital Experience Monitoring.

Tap into UX Best Practices

Top comments (1)

Collapse
 
judith677 profile image
Judith-Data-Processing-Hacks

Feel free to download esProc SPL and share your feedback with us!

🧮Discord
🧮Reddit

Image of Datadog

Get the real story behind DevSecOps

Explore data from thousands of apps to uncover how container image size, deployment frequency, and runtime context affect real-world security. Discover seven key insights that can help you build and ship more secure software.

Read the Report

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay