Skip to main content

Command Palette

Search for a command to run...

Find the Superset from the Relationship Table - From SQL to SPL #19

Published
1 min read
Find the Superset from the Relationship Table - From SQL to SPL #19
E

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

Problem description & analysis:

The MySQL database has two tables: bag table and bag item relationship table. Some bags are supersets of other bags.

bag

bag_item

Task: Now we need to find the superset for each bag:

Code comparisons:

SQL

SELECT
  base.id AS base_bag_id,
  s.id AS superset_bag_id
FROM bag base
JOIN bag s
   ON s.id <> base.id
  AND NOT EXiSTS (SELECT 1
    FROM bag_item bi
    WHERE bi.bag_id = base.id
      AND NOT EXISTS (SELECT 1
        FROM bag_item si
        WHERE si.item_id = bi.item_id
          AND si.bag_id = s.id
    )
);

Two layers of loop traversal are required here, and SQL requires three layers of nesting combined with JOIN and difficult to understand EXISTS to implement set operations, which is difficult to understand.

SPL: SPL can easily implement it using two-layer loops combined with intuitive set operations. 👉🏻Try.DEMO

A2: Group by bag, but do not aggregate, each group is a set of items including a bag and its contents.

A3: Calculate the target result through a two-layer loop. \ means difference set operation.


Free to Try, Powerful to Use: esProc SPL FREE Download.

E
esProc1y ago

Please feel free to download esProc SPL and share your experience with us!

🎯Discord:https://discord.gg/sxd59A8F2W 🎯Reddit:https://www.reddit.com/r/esProc_Desktop/

More from this blog

Untitled Publication

207 posts