通过事先查询帮助建立连接

发布于 2024-10-26 10:49:10 字数 533 浏览 5 评论 0原文

我有以下数据

Order_ID  Pallet_ID
O1        P1
O2        P1
O2        P2
O3        P2
O3        P3
O4        P4

,其中订单可以位于多个托盘上,并且一个托盘上可以有多个订单。我需要选择组成一个组的订单组,因为它们都共享同一组托盘。在上面的测试数据中,有两个这样的组,{O1,O2,O3}和{O4},因为O1,O2和O3与该组的另一成员至少有一个共同的托盘。

现在我需要一些 SQL 来做到这一点。我尝试过(其中 greg_test 包含上面的数据)

select distinct order_id
from greg_test
start with order_id = :order_id
connect by pallet_id = prior pallet_id

但这给了我一个循环引用错误(ORA-01436 CONNECT BY 用户数据中的循环)。添加 nocycle 没有给出正确的集合。

I have the following data

Order_ID  Pallet_ID
O1        P1
O2        P1
O2        P2
O3        P2
O3        P3
O4        P4

Where orders can be on multiple pallets, and more than one order can be on a pallet. I need to select the group of orders that make up a group, in that they all share the same group of pallets. In the test data above, there are two such groups, {O1,O2,O3} and {O4}, because O1, O2 and O3 have at least one pallet in common with another member of the group.

Now I need some SQL to do this. I tried (where greg_test contains the data above)

select distinct order_id
from greg_test
start with order_id = :order_id
connect by pallet_id = prior pallet_id

But that gave me a circular reference error (ORA-01436 CONNECT BY loop in user data). Adding nocycle didn't give the correct set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-11-02 10:49:19
-- Z lists all order pairs that share a pallet, and also pairs each order with itself
WITH pairs AS (
    -- all pairs of orders on the same pallet
    SELECT DISTINCT a.order_id a, b.order_id b FROM greg_test a, greg_test b 
    WHERE a.pallet_id = b.pallet_id AND a.order_id != b.order_id
  UNION ALL 
    -- pair all orders with themselves
    SELECT DISTINCT order_id a, order_id b FROM greg_test
)
-- Now connect all the pairs
SELECT DISTINCT a FROM pairs 
CONNECT BY NOCYCLE PRIOR a = b 
START WITH a = :order_id

也许有更有效的解决方案。

-- Z lists all order pairs that share a pallet, and also pairs each order with itself
WITH pairs AS (
    -- all pairs of orders on the same pallet
    SELECT DISTINCT a.order_id a, b.order_id b FROM greg_test a, greg_test b 
    WHERE a.pallet_id = b.pallet_id AND a.order_id != b.order_id
  UNION ALL 
    -- pair all orders with themselves
    SELECT DISTINCT order_id a, order_id b FROM greg_test
)
-- Now connect all the pairs
SELECT DISTINCT a FROM pairs 
CONNECT BY NOCYCLE PRIOR a = b 
START WITH a = :order_id

Probably there is a more efficient solution.

奶茶白久 2024-11-02 10:49:17

该查询仅使用单个全表扫描,或者如果有索引则可以使用索引范围扫描。

select distinct order_id
from greg_test
start with order_id = :order_id
connect by nocycle pallet_id = prior pallet_id or order_id = prior order_id;

如果您使用的是 11gR2,这将比上面的通过查询连接运行得更快一些,尽管语法在我看来更奇怪。

with orders(order_id, pallet_id) as
(
    select order_id, pallet_id
    from greg_test
    where order_id = :order_id
    union all
    select greg_test.order_id, greg_test.pallet_id
    from greg_test
    inner join orders
        on greg_test.pallet_id = orders.pallet_id
            or greg_test.pallet_id = orders.pallet_id
) cycle order_id, pallet_id set is_cycle to 'Y' default 'N'
select distinct order_id from orders;

如果您有大量数据,您将需要彻底测试您使用的任何解决方案。分层查询通常会出现严重的性能问题。

This query only uses a single full table scan, or can use index range scans if there are indexes.

select distinct order_id
from greg_test
start with order_id = :order_id
connect by nocycle pallet_id = prior pallet_id or order_id = prior order_id;

If you're on 11gR2, this will run a little faster than the above connect by query, although the syntax is weirder IMO.

with orders(order_id, pallet_id) as
(
    select order_id, pallet_id
    from greg_test
    where order_id = :order_id
    union all
    select greg_test.order_id, greg_test.pallet_id
    from greg_test
    inner join orders
        on greg_test.pallet_id = orders.pallet_id
            or greg_test.pallet_id = orders.pallet_id
) cycle order_id, pallet_id set is_cycle to 'Y' default 'N'
select distinct order_id from orders;

If you have large amounts of data you'll want to thoroughly test whichever solution you use. Hierarchical queries often have serious performance problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文