寻找更高效的 'for loop' sql

looking for more efficient 'for loop' sql

我需要帮助来格式化 'for loop' 的输出。

CREATE OR REPLACE PROCEDURE MYPROCEDURE (MYPARAM VARCHAR2)
AS 
BEGIN
DECLARE
type values_table    IS TABLE OF VARCHAR2(1999);        
    MYVALUES                           values_table;

BEGIN
select subscriptionName || ', ' c.name 
BULK COLLECT INTO MYVALUES
from magazine_subscriptions ms
join customers c on ms.customer_id = c.customer_id
where ms.subscription_id > 0;

  FOR i IN 1..MYVALUES.COUNT LOOP       
        DBMS_OUTPUT.PUT_LINE(MYVALUES(i));
  END LOOP;
END;
END;
/

当我 运行 时,我得到重复的订阅名称。

示例:

Subscription bi-annual, james tills
Subscription bi-annual, jessie beckman
Subscription bi-annual, john sackman
Subscription 6-months, amelia hearts
Subscription 6-months, amanda tillman
Subscription 3-months, sally forth
Subscription 3-months, sadie hawkins
Subscription 3-months, susan beaker

有没有办法得到这个?

Subscription bi-annual
james tills
jessie beckman
john sackman

Subscription 6-months:
amelia hearts
amanda tillman

Subscription 3-months: 
sally forth
sadie hawkins
susan hearst

感谢您阅读本文。

您的代码看起来像 Oracle。您可以将这些名称组合成一个字符串:

select subscriptionName, list_agg(c.name, ',') within group (order b c.name) 
from magazine_subscriptions ms join
     customers c
     on ms.customer_id = c.customer_id
where ms.subscription_id > 0
group by subscriptionName;

分隔符可以是任何东西。这只是用一个逗号来说明。