我试图在sql中找到两个数组之间的交集结果。
我在下面的代码中有一个小疑问是我写了y.extend(32000)。 如果我删除他的行我得到限制错误。我的2d数组中只有9条记录。
由于扩展功能,我无法获得数组中的当前计数,即y.count结果32000
演示代码如下:
DECLARE
type items is table of number;
type item_sets is table of items;
y item_sets;
i number := 0;
v_c items;
cursor c1 is
select distinct item from sales_demo order by item;
BEGIN
y := item_sets();
y.EXTEND(32000);
FOR Z IN c1 LOOP
i := i + 1;
SELECT tid bulk collect into y(i) FROM sales_demo WHERE item = z.item;
END LOOP;
v_c := y(1) multiset intersect y(2); -- i want intersection result between y1 and y2
DBMS_OUTPUT.PUT_LINE((v_c).count);
END;
任何帮助都会有用
答案 0 :(得分:0)
试试这个:
WITH Y1 AS (SELECT TID
FROM SALES_DEMO
WHERE ITEM = (SELECT LEAST(ITEM)
FROM SALES_DEMO))
WITH Y2 AS (SELECT TID
FROM SALES_DEMO
WHERE ITEM = (SELECT *
FROM (SELECT ITEM
FROM SALES_DEMO
WHERE ITEM > LEAST(ITEM)
ORDER BY ITEM)
WHERE ROWNUM = 1)
SELECT *
FROM Y1
INTERSECT Y2
这避免了对数组等的所有麻烦,并让数据库做其擅长的事情。
分享并享受。