Oracle 中的分层查询 - 失控?
我在 Oracle 10 SQL 中有一个分层查询曾经可以工作。但是,我删除了它所基于的物化视图,现在我无法让它正确显示,甚至完全忽略该视图。
原始查询如下所示:
select oh.name, oh.description
, sys_connect_by_path(groupname, ':') "Groups"
, (select count(*)
from ml.lastobsmv
where lastobsmv.hdid = oh.hdid) as obscount
from ml.obshead oh
join ml.hiergrps hg on oh.groupid = hg.groupid
connect by prior hg.groupid = hg.parentid
我认为它仍然有效,但如果没有 Lastobsmv 视图,我无法测试它。
如果我将其缩小到
select oh.name, oh.description
from ml.obshead oh
join ml.hiergrps hg on oh.groupid = hg.groupid
它仍然有效,会返回 41K 记录。但是,当我使用 connect by 子句时,它会失控,返回数百万条记录(我通常必须在获得准确计数之前取消它)。
select oh.name, oh.description
, sys_connect_by_path(groupname, ':') "Groups"
from ml.obshead oh
join ml.hiergrps hg on oh.groupid = hg.groupid
connect by prior hg.groupid = hg.parentid
我是否在这里遗漏了一些非常明显的东西,或者我是否误解了它的工作方式?谢谢。
Vadim,
它应该返回一个观察术语列表以及它们所在的组。例如,
Obshead:
# CYCLE DAYS, number of days in menstrual cycle, 100
HierGrps:
100, 50, Gynecology
50, 10, Tx
10, 0, Basic
应该生成
# CYCLE DAYS, number of days in menstrual cycle, :Basic:Tx:Gynecology
(最终以及该 obs 术语被使用的次数,但我会担心这一点之后)。
I have a hierarchical query in Oracle 10 SQL that used to work. However, I removed the materialized view it was based on, and now I can't get it to come out properly, even leaving that view out altogether.
The original query looked like this:
select oh.name, oh.description
, sys_connect_by_path(groupname, ':') "Groups"
, (select count(*)
from ml.lastobsmv
where lastobsmv.hdid = oh.hdid) as obscount
from ml.obshead oh
join ml.hiergrps hg on oh.groupid = hg.groupid
connect by prior hg.groupid = hg.parentid
I presume it still works, but without the lastobsmv view, I can't test it.
If I trim it down to
select oh.name, oh.description
from ml.obshead oh
join ml.hiergrps hg on oh.groupid = hg.groupid
it still works, returning 41K records. However, when I use the connect by clause, it goes out of control, returning millions of records (I usually have to cancel it before getting an accurate count).
select oh.name, oh.description
, sys_connect_by_path(groupname, ':') "Groups"
from ml.obshead oh
join ml.hiergrps hg on oh.groupid = hg.groupid
connect by prior hg.groupid = hg.parentid
Am I missing something really blatant here, or am I misunderstanding the way this is supposed to work? Thanks.
Vadim,
It should return a list of Observation Terms along with the group they're in. For example,
Obshead:
# CYCLE DAYS, number of days in menstrual cycle, 100
HierGrps:
100, 50, Gynecology
50, 10, Tx
10, 0, Basic
should produce
# CYCLE DAYS, number of days in menstrual cycle, :Basic:Tx:Gynecology
(eventually along with the number of times this obs term has been used, but I'll worry about that later).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我用来测试查询的示例数据:
Here's the sample data I've used to test the query:
我不确定您的原始查询如何工作,但这是我工作的最佳解决方案...
它返回
Gynecology:Tx:Basic
(相反的顺序)。您是否真的使用了
prior hg.groupid = hg.parentid
而不是prior hg.parentid = hg.groupid
?我可能会误解您的测试数据,但是看起来我必须从
groupid=100,parentid=50
开始,然后获取groupid=50parentid=10
?I'm not sure how your original query could work, but this is the best solution I got to work...
It returns
Gynecology:Tx:Basic
though (inverse order).Did you really use
prior hg.groupid = hg.parentid
and notprior hg.parentid = hg.groupid
?I might be misinterpreting your test-data, but it looks like I have to start with
groupid=100, parentid=50
and then getgroupid=50 parentid=10
?看来 JOIN 是在连接之前进行评估的,因此您的一行无法构建层次结构。通过子查询你会得到你想要的:
it seems the JOIN is evaluated before the connect by, so your one row can't build the hierarchy. With a subquery you will get what you want:
Peter 的看起来更优雅,但这是我根据有关表格和数据的以下假设得出的结果:
返回给我的是:
编辑:更新了选择以合并 Vadim 的
parentid = 0
Peter's looks more elegant, but here is what I came up with given the following assumptions about the tables and data:
Which returns for me:
Edit: Updated select to incorporate Vadim's
parentid = 0