DEV Community

Cover image for Flexible Interval Aggregation — From SQL to SPL #5
Judith-Data-Processing-Hacks
Judith-Data-Processing-Hacks

Posted on

3 1 1 1 1

Flexible Interval Aggregation — From SQL to SPL #5

Problem description & analysis:

The range of the ID field in a table of Oracle database is integers 1–2000.

source table

Task: Now we need to group IDs into multiple consecutive intervals, sum up the values within each group, and arrange the calculation results in order of intervals. The intervals are in the form of a sequence of numbers that can be flexibly modified. For example, when the numerical sequence is 10, 20, 100, 1000, 2000, it means grouping and aggregating IDs in the intervals of 1–10, 11–20, 21–100, 101–1000, and 1001–2000. Note that the starting value format of the grouping string is: member of the numerical sequence+1.

expected results

Code comparisons:

SQL:

WITH id_ranges (min_value, max_value) AS (
  SELECT LAG(COLUMN_VALUE, 1, 0) OVER (ORDER BY COLUMN_VALUE) + 1,
         COLUMN_VALUE
  FROM   TABLE(SYS.ODCINUMBERLIST(10,20,100,1000,2000))
)
SELECT r.min_value || '-' || r.max_value AS id_range,
       SUM(value) AS sum_of_values
FROM   dummy_data d
       INNER JOIN id_ranges r
       ON     d.id BETWEEN r.min_value AND r.max_value
GROUP BY r.min_value, r.max_value
order by  r.min_value
Enter fullscreen mode Exit fullscreen mode

SQL does not provide a function to find which interval a value belongs to, so it cannot directly group by intervals. It needs to take a detour to create a temporary interval table, and then group and aggregate after association. The structure is quite complex.

SPL:

SPL code is much simpler and easier to understand: try.DEMO

SPL code script

A1: Load data.

A3: Directly group and aggregate by interval, the pseg function returns the interval number where a certain value is located, without the need for table creation or association, @r represents left-open, right-closed interval.

A4: Change the group number to the specified string format.


🚀 Download open-sourced esProc SPL and explore the simpler approach now: Open-Source Address.

Top comments (1)

Collapse
 
judith677 profile image
Judith-Data-Processing-Hacks

Feel free to share your ideas with us and join the communities to dive into more details!

🥳Discord
🥳Reddit

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay