DEV Community

Cover image for Count Date Ranges per Year — From SQL to SPL #23
Judith-Data-Processing-Hacks
Judith-Data-Processing-Hacks

Posted on

3 1 2 1 1

Count Date Ranges per Year — From SQL to SPL #23

Problem description & analysis:

The x field of the database table example is ID, and the ts field is the time interval.

source table

Task: Now we need to count which years are included in the time interval of each ID, and how many days are included in each year.

expected results

Code comparisons:

SQL

WITH RECURSIVE days as (
  SELECT x, LOWER(ts) as t FROM example 
    UNION ALL
  SELECT x, t+'1 day' FROM days 
  where t < (SELECT UPPER(ts) FROM example where x=days.x)
  )
SELECT x, extract(year from t), count(*)
FROM days
GROUP BY x,extract(year from t)
ORDER BY x,extract(year from t)
Enter fullscreen mode Exit fullscreen mode

Common databases do not have data types related to time intervals, making it difficult to break down data. PostgreSQL has tsrange and daterange types, making the code relatively easy to write. However, it also requires recursive subqueries to generate date sequences, which have complex structures and are not easy to understand.

SPL: SPL can directly generate date sequences 👉🏻:Try.DEMO

SPL code

A1: Load data. […] will be parsed as a sequence.

A2: Generate a date sequence using the ts field of each record, and then expand the members of the sequence to form a new two-dimensional table with the x field of this record. The function ‘periods’ generates a sequence based on the start and end dates, and (1) represents the first member of the sequence.


esProc SPL FREE Download — Free Trial Available, Download Now!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
judith677 profile image
Judith-Data-Processing-Hacks

Feel free to download SPL and share your data processing experience with the communities!

🌟Discord
🌟Reddit

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay