CONNECT BY 用于具有两个 JOINS 的两个表
我有 3 个表:
- 其中两个具有分层结构
(如递归类型层次结构的“维度”); - 一种具有求和数据的方法(例如带有 X 列的“事实”)。
它们在这里:
- DIM1 (ID1, PARENT2, NAME1)
- DIM2 (ID2, PARENT2, NAME2)
- FACTS (ID1, ID2, X)
DIM1 表示例:
-- 1 0 DIM1
---- 2 1 DIM1-A
------ 3 2 DIM1-A-A
-------- 4 3 DIM1-A-A-A
-------- 5 3 DIM1-A-A-B
------ 6 2 DIM1-A-B
-------- 7 6 DIM1-A-B-A
-------- 8 6 DIM1-A-B-B
------ 9 2 DIM1-A-C
---- 10 1 DIM1-B
------ 11 10 DIM1-B-C
------ 12 10 DIM1-B-D
---- 13 1 DIM1-C
DIM2 表示例:
-- 1 0 DIM2
---- 2 1 DIM2-A
------ 3 2 DIM2-A-A
-------- 4 3 DIM2-A-A-A
-------- 5 3 DIM2-A-A-B
-------- 6 3 DIM2-A-B-C
------ 7 2 DIM2-A-B
---- 8 1 DIM2-B
---- 9 1 DIM2-C
FACTS 表示例:
1 1 100
1 2 30
1 3 500
-- ................
13 9 200
我想创建 < strong>唯一 SELECT
,我将在其中指定 DIM1 的父级(例如 DIM1-A 的 ID1=2
)和 DIM2 的父级(例如 < code>ID2=2 for DIM2-A) 和 SELECT
将生成如下报告:
Name_of_1 Name_of_2 Sum_of_X
--------- --------- ----------
DIM1-A-A DIM2-A-A (some sum)
DIM1-A-A DIM2-A-B (some sum)
DIM1-A-B DIM2-A-A (some sum)
DIM1-A-B DIM2-A-B (some sum)
DIM1-A-C DIM2-A-A (some sum)
DIM1-A-C DIM2-A-B (some sum)
我想使用 CONNECT BY
短语,START WITH
短语、SUM
短语、GROUP BY
短语以及 OUTER
或 INNER
(?) <代码>加入。我不需要 Oracle 10.2 的其他扩展。
换句话说:仅使用“经典”SQL 和
仅适用于层次结构查询的 Oracle 扩展。
是否可以?
我尝试了一些带有问题的实验
与 Oracle 混合使用 Connect by、内连接和求和< /a>
(这里有一个非常很好的解决方案,但仅适用于一个
维度表(“任务”),但我需要JOIN
两个维度表到一个事实表),但我没有成功。
I have 3 tables:
- two with hierarchical structures
(like "dimensions" of recursive type of hierarchy); - one with summing data (like "facts" with X column).
They are here:
- DIM1 (ID1, PARENT2, NAME1)
- DIM2 (ID2, PARENT2, NAME2)
- FACTS (ID1, ID2, X)
Example of DIM1 table:
-- 1 0 DIM1
---- 2 1 DIM1-A
------ 3 2 DIM1-A-A
-------- 4 3 DIM1-A-A-A
-------- 5 3 DIM1-A-A-B
------ 6 2 DIM1-A-B
-------- 7 6 DIM1-A-B-A
-------- 8 6 DIM1-A-B-B
------ 9 2 DIM1-A-C
---- 10 1 DIM1-B
------ 11 10 DIM1-B-C
------ 12 10 DIM1-B-D
---- 13 1 DIM1-C
Example of DIM2 table:
-- 1 0 DIM2
---- 2 1 DIM2-A
------ 3 2 DIM2-A-A
-------- 4 3 DIM2-A-A-A
-------- 5 3 DIM2-A-A-B
-------- 6 3 DIM2-A-B-C
------ 7 2 DIM2-A-B
---- 8 1 DIM2-B
---- 9 1 DIM2-C
Example of FACTS table:
1 1 100
1 2 30
1 3 500
-- ................
13 9 200
And I would like to create the only SELECT
where I will specify the parent for DIM1 (for example ID1=2
for DIM1-A) and parent for DIM2 (for example ID2=2
for DIM2-A) and SELECT
will generate a report like this:
Name_of_1 Name_of_2 Sum_of_X
--------- --------- ----------
DIM1-A-A DIM2-A-A (some sum)
DIM1-A-A DIM2-A-B (some sum)
DIM1-A-B DIM2-A-A (some sum)
DIM1-A-B DIM2-A-B (some sum)
DIM1-A-C DIM2-A-A (some sum)
DIM1-A-C DIM2-A-B (some sum)
I would like to use CONNECT BY
phrase, START WITH
phrase, SUM
phrase, GROUP BY
phrase, and OUTER
or INNER
(?) JOIN
. I need no other extensions of Oracle 10.2.
In other words: only with "classic" SQL and
only Oracle extensions for hierarchy queries.
Is it possible?
I tried some experiments with question in
Mixing together Connect by, inner join and sum with Oracle
(where is a very nice solution but only for one
dimension table ("Tasks"), but I need to JOIN
two dimension tables to one facts table), but I was not successful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“Some sum”不是很具有描述性,所以我根本不明白为什么你需要
CONNECT BY
。"Some sum" is not very descriptive, so I don't see why do you need
CONNECT BY
at all.我认为您想要做的是获取事实表中按最上面的子项分组的指定行的所有子项的值的总和。这意味着在上面的示例中,第一行的结果将是 (DIM1-AA、DIM1-AAA、DIM1-AAB) 和 (DIM2-AA、DIM2-AAA、DIM2-AAB、DIM3 -AAC) 在 FACTS 表中找到。有了这个假设,我得出了以下解决方案:(
这也假设 FACTS 的列被命名为 ID1、ID2 和 X。)
I think what you're trying to do is get the sum of the value in the facts table for all of the children of the specified rows grouped by the topmost children. This would mean that in your example above, the results for the first row would be the sum any intersections of (DIM1-A-A, DIM1-A-A-A, DIM1-A-A-B) and (DIM2-A-A, DIM2-A-A-A, DIM2-A-A-B, DIM3-A-A-C) found in the FACTS table. With that assumption, I have come to the following solution:
(This also assumes that the columns of FACTS are named ID1, ID2, and X.)