oracle子查询问题与Rownum和Order in where子句中的订单无效的外部表格
我正在尝试找到一种解决SQL查询的方法。我想做一个子查询以首先选择产品ID的启用文章。实际上,我正在研究查询的一部分。我只需要一篇文章作为一个产品,或者如果没有启用文章,则最终禁用了最后一个禁用。问题在于,我被要求不使用加入而不使用请求加入。这是传统应用。
这是一个工作示例:
from T_PRODUIT pro, T_PRODUIT_PLATEFORME_EXTENDED pre, T_ARTICLE art, T_TAUX_TVA tva
where pro.id_produit = 1330442
and art.id_article in (select id_article from T_ARTICLE ta where ta.id_produit = pro.id_produit and ta.id_fournisseur = pre.id_fournisseur_article)
and pro.ID_PRODUIT = pre.ID_PRODUIT
and pre.ID_PRODUIT = art.ID_PRODUIT(+)
and pre.ID_FOURNISSEUR_ARTICLE = art.ID_FOURNISSEUR(+)
and tva.CODE = pro.ID_TVA
我想要的是:
from T_PRODUIT pro, T_PRODUIT_PLATEFORME_EXTENDED pre, T_ARTICLE art, T_TAUX_TVA tva
where pro.id_produit = 1330442
and art.id_article in (select * from (select id_article from T_ARTICLE ta where ta.id_produit = pro.id_produit and ta.id_fournisseur = pre.id_fournisseur_article order by ta.actif DESC) where rownum < 2)
and pro.ID_PRODUIT = pre.ID_PRODUIT
and pre.ID_PRODUIT = art.ID_PRODUIT(+)
and pre.ID_FOURNISSEUR_ARTICLE = art.ID_FOURNISSEUR(+)
and tva.CODE = pro.ID_TVA
第二个示例的问题是,在这种情况下,有两个嵌套级别,Pro和Pre是无效的标识符。有人知道我如何绕过这个问题? 该数据库有时包含引起复杂SQL的奇怪行。 谢谢。
I'm trying to find a way to resolve an sql query. I want to do a subquery to select enable articles for a product id in first. In fact i'm working on a part of a query. I would want just one article for a product the last enable or else the last disable if no enable articles. The problem is that i was asked to not use join with request without join at first. It's legacy app.
Here is a working example :
from T_PRODUIT pro, T_PRODUIT_PLATEFORME_EXTENDED pre, T_ARTICLE art, T_TAUX_TVA tva
where pro.id_produit = 1330442
and art.id_article in (select id_article from T_ARTICLE ta where ta.id_produit = pro.id_produit and ta.id_fournisseur = pre.id_fournisseur_article)
and pro.ID_PRODUIT = pre.ID_PRODUIT
and pre.ID_PRODUIT = art.ID_PRODUIT(+)
and pre.ID_FOURNISSEUR_ARTICLE = art.ID_FOURNISSEUR(+)
and tva.CODE = pro.ID_TVA
and what i would want :
from T_PRODUIT pro, T_PRODUIT_PLATEFORME_EXTENDED pre, T_ARTICLE art, T_TAUX_TVA tva
where pro.id_produit = 1330442
and art.id_article in (select * from (select id_article from T_ARTICLE ta where ta.id_produit = pro.id_produit and ta.id_fournisseur = pre.id_fournisseur_article order by ta.actif DESC) where rownum < 2)
and pro.ID_PRODUIT = pre.ID_PRODUIT
and pre.ID_PRODUIT = art.ID_PRODUIT(+)
and pre.ID_FOURNISSEUR_ARTICLE = art.ID_FOURNISSEUR(+)
and tva.CODE = pro.ID_TVA
The problem with the second example is that there are two nesting level and pro and pre are invalid identifiers in this case. Someone know how i can bypass this problem ?
The database sometimes contains strange rows causing complex sql.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Oracle 12中,您可以使用
fet fetter第一行
语法:注意:您可能需要在过滤条件中移动该
on
on 加入条款,使其是外部联接条件的一部分;否则,您将有效地将外部Join
转换为内在
。在早期版本中,您可以使用:
注意:再次,您可能需要移动它进入联接条件。
,您可能想要的是:
或者,使用遗产加入:
From Oracle 12, you can use the
FETCH FIRST ROW ONLY
syntax:Note: You may want to move that
IN
filter condition to theON
clause of the join so that it is part of the outer join condition; otherwise you are effectively converting theOUTER JOIN
to anINNER JOIN
.In earlier versions you can use:
Note: again, you may want to move it into the join condition.
However, what you probably want is:
Or, using the legacy joins: