困难的层次查询和连接
(注:这里的数据是一个虚构的例子,因为我无法发布真实的数据。争论表结构没有意义或应该改变是没有用的)
数据库是Oracle 10g。
给定:
Products
------------
ID
Price
Sample Data:
ID Product_ID Customer Quantity
-------------------------
1 30 2 10
2 40 2 15
3 50 5 7
4 60 5 9
Product_types
-----------
ID
Name
Parent
Type
Data
ID Product_ID Name Parent
----------------------------------
1 10 Box
2 20 Toolbox 10
3 30 Hammer 20
4 40 Nail 30
查询:
select * from (select * from Product_types t
START WITH t.Parent = 20
CONNECT BY PRIOR t.Product_ID = t.PARENT) t_result
left join Products p on T_RESULT.Product_ID = P.Product_ID
where P.Customer = 2;
当前输出:
Product_ID Name Parent Product_ID_1 Customer Quantity
-------------------------------------------------------------------
30 Hammer 20 30 2 10
40 Nail 30 40 2 15
期望输出:
Product_ID Name Parent Product_ID_1 Customer Quantity
---------------------------------------------------------------------
20 Toolbox 10
30 Hammer 20 30 2 10
40 Nail 30 40 2 15
我意识到由于我的 where 子句,它只选择 customer = 2 的行。但是,我想知道是否有任何方法也可以到达层次结构的顶部。我的第一个想法是左连接仍应为工具箱行提供 NULL 客户,但该行不包括在内。我还尝试过完整的外部连接。
我怀疑我可能必须运行两个查询并手动组合结果才能获得我想要的结果,但想先咨询一些专家。
任何人都可以想出一种方法来获得所需的输出吗?
(Note: the data here is a made up example since I can't post the real data. No use arguing that the table structure doesn't make sense or should change)
Database is Oracle 10g.
Given:
Products
------------
ID
Price
Sample Data:
ID Product_ID Customer Quantity
-------------------------
1 30 2 10
2 40 2 15
3 50 5 7
4 60 5 9
Product_types
-----------
ID
Name
Parent
Type
Data
ID Product_ID Name Parent
----------------------------------
1 10 Box
2 20 Toolbox 10
3 30 Hammer 20
4 40 Nail 30
Query:
select * from (select * from Product_types t
START WITH t.Parent = 20
CONNECT BY PRIOR t.Product_ID = t.PARENT) t_result
left join Products p on T_RESULT.Product_ID = P.Product_ID
where P.Customer = 2;
Current Output:
Product_ID Name Parent Product_ID_1 Customer Quantity
-------------------------------------------------------------------
30 Hammer 20 30 2 10
40 Nail 30 40 2 15
Desired Output:
Product_ID Name Parent Product_ID_1 Customer Quantity
---------------------------------------------------------------------
20 Toolbox 10
30 Hammer 20 30 2 10
40 Nail 30 40 2 15
I realize it's only selecting the rows with customer = 2 because of my where clause. However, I wonder if there's any way to get the top of the hierarchy as well. My first thought was a left join should still provide the Toolbox row with a NULL customer, but the row is not included. I've also tried a full outer join.
I suspect I may be in the realm of having to run two queries and combine the results manually to get my desired result, but wanted to consult some experts first.
Can anyone come up with a way to get the desired output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有效吗?
注意:我将最后一行的 WHERE 更改为 AND,以使条件成为外连接的一部分。
Does this work?
Note: I changed WHERE to AND on the last line to make the condition part of the outer join.
此外,您还应该更改递归的起点:
或者甚至使用 START WITH t.Parent IS NULL 来获取完整的递归。
Also you should change the start point of the recursion:
Or even use
START WITH t.Parent IS NULL
to get the full recursion.