Summarize Row Counts by Dynamic Columns into JSON โ From SQL to SPL #22


Problem description & analysis:
Each column of database table tempdata can be viewed as an array, with duplicate members:
Task: Now we need to group and count each column separately, in JSON format:
Code comparisons:
SQL:
With Grouped AS (
SELECT
td.hobbies_1,
td.hobbies_2,
COUNT(*) AS count
FROM _temp_data td
GROUP BY GROUPING SETS (
(td.hobbies_1),
(td.hobbies_2)
)
)
SELECT
jsonb_object_agg(g.hobbies_1, g.count) FILTER (WHERE g.hobbies_1 IS NOT NULL) AS hobbies_1,
jsonb_object_agg(g.hobbies_2, g.count) FILTER (WHERE g.hobbies_2 IS NOT NULL) AS hobbies_2
FROM Grouped g;
First uses GROUPING SETS to group and count different columns simultaneously, and then uses jsonb_object_agg to convert each record set into JSON separately. The code is quite cumbersome; And the column names must be written, which is less flexible; If you want to support dynamic column names, you need to use stored procedures, and the structure will become complex.
SPL: SPL does not need to write column names: ๐๐ปTry.DEMO.
A1๏ผ Load data.
A2๏ผ Transpose the two-dimensional table A1 into a sequence of sequences, group and count each small sequence (corresponding to each column), and then convert it into Json. Function E is used to convert between sequences and table sequences. @p represents the transpose of the two-layer sequence, @b represents removing the column name. When converting a set of records to JSON, there is an extra [] symbol compared with converting a record. Here, (1) is used to take the unique record and then convert it.
A3๏ผ Create a new empty two-dimensional table according to the A1 structure and fill in the Json sequence A2.
Experience esProc SPL โ Free Trial, No Hassle! ๐๐ป esProc SPL FREE Download
Subscribe to my newsletter
Read articles from esProc directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

esProc
esProc
esProc SPL is a JVM-based programming language designed for structured data computation, serving as both a data analysis tool and an embedded computing engine. FREE download๐๐ป: https://www.esproc.com/download-esproc