 
--  首先只获取一次 得到有transfer其他add 的 add 下载下来 通过py脚本进行第一部筛选
-- 对有transfer的多个add 进行输入递归查询 
WITH RECURSIVE excludeAdd (exclude_add) as (
       values
            ('add21')
            ,('add20')
),
input_add(_add)as
(
    values
            ('add0')
            ,('add1')
)
 
 cte AS (
    SELECT
  
     ID
    , time
    , from_add
    , to_add
    , data
    ,    1 AS level
 
    FROM table_01
    WHERE from_add IN (select _add from   input_add ) OR to_add IN (select _add from   input_add ) 
    UNION ALL
    SELECT 
      
   t.ID
    , t.time
    , t.from_add
    , t.to_add
    , t.data
    ,   cte.level + 1
 
    FROM table_01 t
    JOIN cte ON t.from_add = cte.to_add OR t.to_add = cte.from_add
    WHERE 
 
      (SELECT COUNT(*) FROM table_01 sub WHERE sub.from_add = t.from_add OR sub.to_add = t.from_add) <= 10
          AND t.from_add NOT IN (SELECT exclude_add FROM excludeAdd) 
   AND t.to_add NOT IN (SELECT exclude_add FROM excludeAdd)
   -- 递归次数小于5
   AND level <=4 
) 

SELECT DISTINCT  ID, time, from_add, to_add, data,    level
 
FROM cte;

 