【问题标题】:sql DB2 query to enumerate all paths from edgelistsql DB2查询枚举edgelist中的所有路径
【发布时间】:2016-07-08 03:29:28
【问题描述】:

我在 DB2 中创建了一个表:

CREATE TABLE mytable
(
  loan_id integer,
  client_id integer 
);

在表格中插入值:

INSERT INTO mytable(
loan_id, client_id)
VALUES (1, '2');

最后表格包含以下数据:

loan_id     client_id
1           2
1           4
4           2
2           3

我想运行一个 SQL 查询来枚举(打印)所有唯一可能的路径。下面给出了一个示例输出:

1 -> 2
1 -> 4
4 -> 2
2 -> 3
1 -> 2 -> 3
4 -> 1 -> 2
4 -> 2 -> 3
4 -> 1 -> 2 -> 3
3 -> 2 -> 4 -> 1

我查看了以下answer 并尝试了以下代码,但出现错误:

WITH links AS
( SELECT 
    loan_id, 
    client_id as c1, 
    client_id as c2, 0 as distance 
  FROM 
    mytable 
  -- recursion 
  UNION ALL
  SELECT 
     t.loan_id, 
     l.c1 as c1, 
     tt.client_id as c2, 
     distance+ as distance 
  FROM 
    links l INNER JOIN
    myTable t ON l.c2 = t.client_id
    AND l.loan_id != t.loan_id INNER JOIN
    myTable tt  ON t.loan_id = tt.loan_id 
     AND t.client_id != tt.client_id
 )
SELECT * FROM mytable t
WHERE EXISTS 
    (SELECT * FROM links 
     WHERE c2 = t.client_id and c1 = 3);

感谢您的帮助。

【问题讨论】:

  • 它给出了哪个错误?

标签: sql database db2 recursive-query


【解决方案1】:

您可以在函数 SYS_CONNECT_BY_PATH 中搜索

example here

【讨论】:

    【解决方案2】:

    在 SQL SERVER 版本中:

    WITH allvalue as ( select loan_id id from mytable union select client_id id from mytable ), allvaluewithnbcombi as ( select f1.*, (select count(*) from allvalue f2) nbcombi from allvalue f1 ) , tmprecurse (id, pathcalcul, rang, nbcombi) as ( select id , cast('' as varchar(50)) as pathcalcul, 1 rang, nbcombi from allvaluewithnbcombi union all select f1.id , cast(case when pathcalcul='' then cast(f2.id as varchar(50)) else f2.pathcalcul + '->' + cast(f2.id as varchar(50)) end as varchar(50)) pathcalcul,
    f2.rang + 1, f1.nbcombi from allvaluewithnbcombi f1, tmprecurse f2 where f1.id<>f2.id and f2.rang<=f1.nbcombi ) , resultat as ( select distinct pathcalcul from tmprecurse where pathcalcul<>'' and pathcalcul like '%->%' ) select * from resultat order by len(pathcalcul)

    在 DB2 版本中(未测试)

    WITH allvalue as ( select loan_id id from mytable union select client_id id from mytable ), allvaluewithnbcombi as ( select f1.*, (select count(*) from allvalue f2) nbcombi from allvalue f1 ) , tmprecurse (id, pathcalcul, rang, nbcombi) as ( select id , cast('' as varchar(50)) as pathcalcul, 1 rang, nbcombi from allvaluewithnbcombi union all select f1.id , cast(case when pathcalcul='' then cast(f2.id as varchar(50)) else f2.pathcalcul concat '->' concat cast(f2.id as varchar(50)) end as varchar(50)) pathcalcul,
    f2.rang + 1, f1.nbcombi from allvaluewithnbcombi f1, tmprecurse f2 where f1.id<>f2.id and f2.rang<=f1.nbcombi ) , resultat as ( select distinct pathcalcul from tmprecurse where pathcalcul<>'' and pathcalcul like '%->%' ) select * from resultat order by len(pathcalcul)

    希望对你来说是正确的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 2010-12-09
      • 1970-01-01
      • 2018-08-29
      相关资源
      最近更新 更多