带有 FIRST 选项的 Informix 子查询
将以下 Transact-SQL 代码转录到 Informix Dynamic Server (IDS) 9.40 的最佳方法是什么:
目标:我需要前 50 个订单及其各自的订单行
select *
from (select top 50 * from orders) a inner join lines b
on a.idOrder = b.idOrder
我的问题在于子选择,因为 Informix 不允许使用 FIRST 选项子选择。
有什么简单的想法吗?
What is the best way of transcribing the following Transact-SQL code to Informix Dynamic Server (IDS) 9.40:
Objective: I need the first 50 orders with their respective order lines
select *
from (select top 50 * from orders) a inner join lines b
on a.idOrder = b.idOrder
My problem is with the subselect because Informix does not allow the FIRST option in the subselect.
Any simple idea?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
官方的回答是“请从 IDS 9.40 升级,因为 IBM 不再支持它”。 也就是说,IDS 9.40 不是当前版本 - 并且(理想情况下)不应使用。
IDS 11.50 的解决方案
使用 IDS 11.50,我可以写:
这或多或少相当于您的查询。 因此,如果您使用当前版本的 IDS,则可以使用与 Transact-SQL 中几乎相同的表示法编写查询(使用 FIRST 代替 TOP)。
IDS 9.40 的解决方案
您可以在 IDS 9.40 中做什么? 打扰一下...我必须运行我的 IDS 9.40.xC7 服务器(此修复包于 2005 年发布;原始版本可能于 2003 年末)...
第一个问题 - IDS 9.40 不允许子查询在 FROM 子句中。
第二个问题 - IDS 9.40 不允许在以下任一上下文中使用“FIRST n”表示法:
第三个问题 - IDS 9.40 没有简单的 ROWNUM。
因此,为了解决这些问题,我们可以编写(使用临时表 - 我们稍后将删除它):
这会产生与 IDS 11.50 中的单个查询相同的答案。 我们可以避免使用临时表吗? 是的,但它更冗长:
将其应用于原始订单和订单行示例留给读者作为练习。
“元素表”架构的相关子集:
输出(在我的示例数据库上):
The official answer would be 'Please upgrade from IDS 9.40 since it is no longer supported by IBM'. That is, IDS 9.40 is not a current version - and should (ideally) not be used.
Solution for IDS 11.50
Using IDS 11.50, I can write:
This is more or less equivalent to your query. Consequently, if you use a current version of IDS, you can write the query using almost the same notation as in Transact-SQL (using FIRST in place of TOP).
Solution for IDS 9.40
What can you do in IDS 9.40? Excuse me a moment...I have to run up my IDS 9.40.xC7 server (this fix pack was released in 2005; the original release was probably in late 2003)...
First problem - IDS 9.40 does not allow sub-queries in the FROM clause.
Second problem - IDS 9.40 does not allow 'FIRST n' notation in either of these contexts:
Third problem - IDS 9.40 doesn't have a simple ROWNUM.
So, to work around these, we can write (using a temporary table - we'll remove that later):
This produces the same answer as the single query in IDS 11.50. Can we avoid the temporary table? Yes, but it is more verbose:
Applying that to the original orders plus order lines example is left as an exercise for the reader.
Relevant subset of schema for 'Table of Elements':
Output (on my sample database):
如果我理解你的问题,那么你对“TOP”有疑问。 尝试使用 TOP-N 查询。
例如:
这将为您提供前五十个结果(因为我在子查询中按 desc 排序)
If I understand your question you are having a problem with "TOP". Try using a TOP-N query.
For example:
This will get you the top fifty results (because I order by desc in the sub query)