通过事先查询帮助建立连接
我有以下数据
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许有更有效的解决方案。
Probably there is a more efficient solution.
该查询仅使用单个全表扫描,或者如果有索引则可以使用索引范围扫描。
如果您使用的是 11gR2,这将比上面的通过查询连接运行得更快一些,尽管语法在我看来更奇怪。
如果您有大量数据,您将需要彻底测试您使用的任何解决方案。分层查询通常会出现严重的性能问题。
This query only uses a single full table scan, or can use index range scans if there are indexes.
If you're on 11gR2, this will run a little faster than the above connect by query, although the syntax is weirder IMO.
If you have large amounts of data you'll want to thoroughly test whichever solution you use. Hierarchical queries often have serious performance problems.