P
P
prochanev2020-01-22 13:54:47
PostgreSQL
prochanev, 2020-01-22 13:54:47

How to find longest prefix among strings and group by it?

Hello! Already spammed the whole Google, but what he gave out, he could not apply.
I could only find that a similar task is called Longest common prefix(LCP)
There is an example table:

+----+-----------+------------------------+
| id | parent_id | path                   |
+----+-----------+------------------------+
| 1  | 7         | val10/val11/val12/val3 |
| 2  | 7         | val1/val2/val3/val5    |
| 3  | 7         | val1/val2/val3/val6    |
| 4  | 7         | val1/val2/val3/val7    |
| 5  | 7         | val1/val2/val3/val8    |
| 6  | 7         | val1/val2/val3/val9    |
+----+-----------+------------------------+

How to group rows by longest prefix in path column relative to parent_id? The prefix means the repeating part of the string starting from the beginning.
For the table above, the output should be:
+-----------+------------------------+-------+
| parent_id | path                   | count |
+-----------+------------------------+-------+
| 7         | val1/val2/val3         | 5     |
| 7         | val10/val11/val12/val3 | 1     |
+-----------+------------------------+-------+

to generate an example:
create table example (id int, parent_id int, path varchar(50));

insert into example
select 1, 7, 'val10/val11/val12/val3'
union select 2, 7, 'val1/val2/val3/val5'
union select 3, 7, 'val1/val2/val3/val6'
union select 4, 7, 'val1/val2/val3/val7'
union select 5, 7, 'val1/val2/val3/val8'
union select 6, 7, 'val1/val2/val3/val9'

Perhaps someone met with such a task, respond! )

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Tsvetkov, 2020-01-23
@prochanev

tree .

D
d-stream, 2020-01-22
@d-stream

um,,, select path, count(*) from example group by path ?
Or am I not quite understanding something?

M
mayton2019, 2020-01-23
@mayton2019

As for me - the task is set incorrectly. Or there are not enough additional conditions. For example, I think that the general prefix should be like this.

+-----------+------------------------+-------+
| parent_id | path                   | count |
+-----------+------------------------+-------+
| 7         | val1                   | 6     |
+-----------+------------------------+-------+

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question