【问题标题】:Oracle PL/SQL looping through undefined number of groupsOracle PL/SQL 循环遍历未定义数量的组
【发布时间】:2016-09-09 11:54:57
【问题描述】:

我正在为以下数据集编写一个 oracle PL/SQL 过程。

Column1---Column 2---Column 3
ID-1      Group1     Item 11
ID-1      Group1     Item 12
ID-1      Group1     Item 13
ID-1      Group2     Item 21
ID-1      Group2     Item 22
ID-2      Group3     Item 31
ID-2      Group3     Item 32
ID-2      Group4     Item 41
ID-2      Group5     Item 51
ID-2      Group5     Item 52

根据输入的 id,我会得到几个组,每个组中的项目很少。我需要一个如下连接的结果集:

输入:ID-1 单列输出:

Item 11 - Item 21
Item 11 - Item 22
Item 12 - Item 21
Item 12 - Item 22
Item 13 - Item 21
Item 13 - Item 22

输入:ID-2 单列输出:

Item 31 - Item 41 - Item 51
Item 31 - Item 41 - Item 52
Item 32 - Item 41 - Item 51
Item 32 - Item 41 - Item 52

输出应包含属于输入 ID 的每个组中的一项的所有组合。 注意ID下的组数不是固定的,组内的项数也不是固定的。

【问题讨论】:

  • 您说您正在编写代码 - 请让我们看看您已经拥有什么。

标签: oracle plsql logic


【解决方案1】:

使用dense_rank() 为您的组编号,然后使用此列在分层查询中的connect by 子句中进行通信:

select ltrim(sys_connect_by_path(column3, ' - '), ' - ') as list 
  from (select dense_rank() over (order by column2) rnk, column1, column2, column3
          from yourtable where column1 = 'ID-1')   
  where connect_by_isleaf = 1
  connect by prior rnk + 1 = rnk 
  start with rnk = 1

测试数据和输出:

create table yourtable (Column1 varchar2(10), Column2 varchar2(10), Column3 varchar2(10));
insert into yourtable values( 'ID-1', 'Group1', 'Item 11');
insert into yourtable values( 'ID-1', 'Group1', 'Item 12');
insert into yourtable values( 'ID-1', 'Group1', 'Item 13');
insert into yourtable values( 'ID-1', 'Group2', 'Item 21');
insert into yourtable values( 'ID-1', 'Group2', 'Item 22');
insert into yourtable values( 'ID-2', 'Group3', 'Item 31');
insert into yourtable values( 'ID-2', 'Group3', 'Item 32');
insert into yourtable values( 'ID-2', 'Group4', 'Item 41');
insert into yourtable values( 'ID-2', 'Group5', 'Item 51');
insert into yourtable values( 'ID-2', 'Group5', 'Item 52');

LIST
--------------------
Item 11 - Item 21
Item 11 - Item 22
Item 12 - Item 21
Item 12 - Item 22
Item 13 - Item 21
Item 13 - Item 22    

【讨论】:

  • 这几乎是完美的,但经常遇到 ORA-01489: result of string concatenation is too long 错误。是否可以使用 XMLAGG 或类似的东西,我可以 clob?谢谢你的帮助!!
  • 我想是的。但这是一个新话题,我建议你也许应该问一个新问题。其他用户可能对此或其他想法有更多的了解。我做了我能做的;-)
【解决方案2】:
select substr(B.perm,4,length(B.perm)) from 
(select level l,sys_connect_by_path(A.Column3, ' - ') as perm , sys_connect_by_path(A.Column2, ' - ') ttt
from (select t.* from yourTable t where t.Column1 = 'ID-2')A -- ID-2 is your input
connect by level <= 3 -- 3 is your distinct group count for your input
order by level)B
where B.l = 3 and instr(B.ttt , 'Group3 - Group4 - Group5') > 0 -- 3 is your distinct group count for your input , *** Group3 - Group4 - Group5 is your merged data according to your input
order by substr(B.perm,4,length(B.perm))asc

我测试了它,如果你设置的值正确,它对两个 ID 都有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2018-12-06
    • 2014-09-27
    • 2021-11-25
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多