-5

我有一个简单的查询:

select key, name from cities

这给了我这样的价值观:

11 | Chicago
21 | New York
31 | Boston

我需要做的是在 SQL 或 Java 中创建所有可能组合的哈希,以便在最后收到此结果:

11 | Chicago
21 | New York
31 | Boston
32 | Chicago/New York
42 | Chicago/Boston
52 | New York/Boston
63 | Chicago/New York/Boston

有任何想法吗?

4

1 回答 1

0

这并不完美,但可能是一个开始:

首先我创建了一个函数:

create or replace function sum_string(str in varchar2) return number is
  Result number;
begin
  execute immediate 'select ' || str || ' from dual'
    into result;
  return(Result);
end sum_string;
/

然后是查询(your_table是你的表)

with jwt as  (
  select t1.key k1, t1.name n1, t2.key k2, t2.name n2, t1.key + t2.key jkey
    from your_table t1
    left outer join (select key, name
                       from your_table
                     union all
                     select 0 "key", null "name" from dual) t2 on t1.key <>
                                                                  t2.key
  )
select jkey, max(aaa.pth)
  from (select sys_connect_by_path(t.n1, '/') pth,
               sum_string(ltrim(sys_connect_by_path(t.k1, '+'), '+')) jkey
          from jwt t
         start with t.k2 = 0
        connect by nocycle prior k1 = k2) aaa
 group by jkey;
于 2012-06-03T12:11:37.743 回答