Skip to content
Advertisement

Group items by 2 columns or similarity with another column

Let’s say we have a Sqlite table containing:

How to group by h=CONCAT(city,age) but also add in the group the users that have a column foo identical to someone in the group?

As seen in Group items by 2 columns, here is how to group by h:

How to add this second condition on foo?

Example: here Alice and Bob are in the same group1 because they have identical h=CONCAT(city,age), but Jack should also be in group1 because he has same foo value as Alice: 12. Simone has same CONCAT(city,age) than Jack, therefore she is in the same group as Jack, in group1 as well.

Advertisement

Answer

This is much trickier. This is a graph-walking problem, which requires a recursive CTE (Common Table Expression).

The idea is to generate all the edges between names. Then use a recursive CTE to visit all connected nodes (names), avoiding visiting existing ones.

Here is one method:

And here is a db<>fiddle.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement