如何将结果与父字符串隔离?

时间:2019-06-15 23:06:45

标签: sql postgresql

您有想法如何将结果从以下任务中分离出来。.

我有一列违反以下值:

col_1
10001A
10001A10002A
10001A10002A10003A
10004A
10004A10005B
10006A
10007A
10007A10008A

我应该只选择没有后代的行-

col_1
10001A10002A10003A
10004A10005B
10006A
10007A10008A

1 个答案:

答案 0 :(得分:2)

您需要一个类似的条件才能找到这些行:

select *
from the_table t1 
where not exists (select *
                  from the_table t2
                  where t2.col_1 like t1.col_1||'%'
                   and t1.col_1 <> t2.col_2);

在线示例:https://rextester.com/GVGVV77242