DEV Community

Judy
Judy

Posted on

2 1 1 1 1

SQL, extract unique values of JSON format field from each group #eg42

Below is a table in PostgreSQL. Its 1st column contains IDs, according to which rows are ordered. The 2nd column is the grouping column. The 3rd column contains JSON-like values (non-standard JSON), where there are duplicate items.

Image description
Use Java to do this: Extract unique items of the 3rd column values from each group and keep them at their original positions in the original records. The task can be interpreted like this: According to the order of records in each group, delete duplicate items downward and keep the unique items at their original positions.

Image description
Write the following SPL code:

Image description
A1: Query the database through JDBC.

A2: Split the 2nd field of each record into multiple items.

A3: Group records by the 1st column while keeping the original order, and handle each group: cumulate all items of the 2nd column for each record according to the order of records and remove duplicates; reverse the order of records; find difference between the cumulative result of the current record and that of the next record.

A4: Restore to the original string format and return the final result.

Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.

This is one of the problems on StackOverflow. You can click on it to see that the conventional solution is quite complicated, but the SPL approach is really simple and efficient.

SPL open source address

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay