0

I have a list of strings:

  1. HEAWAMFWSP
  2. TLHHHAFWSP
  3. AWAMFWHHAW
  4. AUAWAMHHHA

Each of these strings represent 5 pairs of 2 character combinations (i.e. HE AW AM FW SP)

What I am looking to do in SQL is to display all strings that have duplication in the pairs.

Take string number 3 from above; AW AM FW HH AW. I need to display this record because it has a duplicate pair (AW).

Is this possible?

Thanks!

4

2 回答 2

0

鉴于当前的要求,是的,这是可行的。这是一个使用递归 CTE 的版本(文本可能需要针对供应商的特性进行调整),在 DB2 上编写和测试。请注意,如果字符串中有超过 2 个对的实例,或者超过 1 组重复,这将返回多行。

WITH RECURSIVE Pair (rowid, start, pair, text) as (
                     SELECT id, 1, SUBSTR(text, 1, 2), text
                     FROM SourceTable
                     UNION ALL
                     SELECT rowid, start + 2, SUBSTR(text, start + 2, 2), text
                     FROM Pair
                     WHERE start < LENGTH(text) - 1)
SELECT Pair.rowid, Pair.pair, Pair.start, Duplicate.start, Pair.text
FROM Pair
JOIN Pair as Duplicate
ON Duplicate.rowid = Pair.rowid
AND Duplicate.pair = Pair.pair
AND Duplicate.start > Pair.start                                          
于 2012-06-07T16:13:06.983 回答
0

这是一个不太优雅的解决方案,但它有效并且无论有多少重复匹配项都只返回一次该行。substring 函数适用于 SQLServer,不确定它适用于 Oracle。

select ID, Value
from MyTable
where (substring(Value,1,2) = substring(Value,3,4)
or substring(Value,1,2) = substring(Value,5,6)
or substring(Value,1,2) = substring(Value,7,8)
or substring(Value,1,2) = substring(Value,9,10)
or substring(Value,3,4) = substring(Value,5,6)
or substring(Value,3,4) = substring(Value,7,8)
or substring(Value,3,4) = substring(Value,9,10)
or substring(Value,5,6) = substring(Value,7,8)
or substring(Value,5,6) = substring(Value,9,10)
or substring(Value,7,8) = substring(Value,9,10))
于 2012-06-07T16:15:41.033 回答