简体   繁体   English

如何在子查询中使用别名?

[英]How can I use an alias inside a sub-query?

What I am trying to achieve is to get the rows having the maximum value from a table of the following form: 我想要实现的是从以下形式的表中获取具有最大值的行:

A | B | Ratio 
0 | 1 | 1.0
1 | 2 | 1.0
2 | 3 | 1.0
3 | 4 | 0.33
4 | 5 | 0.25

I am trying to display only rows containing the maximum value (in this case 1.0). 我试图只显示包含最大值的行(在本例中为1.0)。 May be I am not doing this right. 可能是我没有这样做。 I have a query of the form: 我有一个表格的查询:

SELECT A,B,C 
FROM (---Long Sub Query--- Aliased as Full_Table)
WHERE RATIO=(SELECT MAX(RATIO) FROM Full_Table);

But Full_Table cannot be referenced from the second sub-query. 但是,无法从第二个子查询引用Full_Table。 There are some rows having the same maximum value which is the reason I was using this query. 有些行具有相同的最大值,这是我使用此查询的原因。 Is there a better construct to achieve this? 有没有更好的结构来实现这一目标? In the worst case, I have to replace the second Full_Table by the entire long query but I'm hoping there is a better way to do this. 在最坏的情况下,我必须用整个长查询替换第二个Full_Table,但我希望有更好的方法来做到这一点。

You can use a Common Table Expression: 您可以使用公用表表达式:

WITH Full_Table AS (---Long Sub Query---)
SELECT A,B,C 
FROM Full_Table
WHERE RATIO=(SELECT MAX(RATIO) FROM Full_Table);

Use: 使用:

SELECT full_table.a,
       full_table.b,
       full_table.c
  FROM (SELECT ...,
               RANK() OVER (ORDER BY ratio DESC) AS rank
          FROM Sub Query---) full_table
 WHERE full_table.rank = 1

It's not clear if there can be more than one record returned, so I used RANK() rather than ROW_NUMBER() because ROW_NUMBER would only return one record. 目前尚不清楚是否可以返回多条记录,因此我使用RANK()而不是ROW_NUMBER()因为ROW_NUMBER只会返回一条记录。

You could incorporate that into a WITH clause, but it's still one pass over the derived table/inline view vs your two passes... 你可以把它合并到一个WITH子句中,但它仍然是派生表/内联视图与你的两次传递的一次传递......

Oracle 9i+ supports the WITH syntax, calling it "Subquery Factoring". Oracle 9i +支持WITH语法,称之为“子查询因子”。 Those coming from SQL Server 2005+ know the WITH syntax as a Common Table Expression (CTE). 来自SQL Server 2005+的那些人将WITH语法知道为公用表表达式(CTE)。 Unlike SQL Server's implementation, the WITH syntax on Oracle 9i - 11g is not recursive - Oracle only added recursive WITH support (now that it's ANSI) in 11g R2, in part due to Oracle supporting recursive functionality with the CONNECT BY syntax (supported since Oracle v2). 与SQL Server的实现不同,Oracle 9i-11g上的WITH语法不是递归的 - Oracle仅在11g R2中添加了递归WITH支持(现在它是ANSI),部分原因是Oracle支持使用CONNECT BY语法的递归功能(自Oracle以来支持) V2)。 WITH syntax is syntactic sugar for derived tables/inline views - the query plan isn't reused for each instance. WITH语法是派生表/内联视图的语法糖 - 查询计划不会为每个实例重用。

WITH full_table AS (
  SELECT...)
SELECT x.a, x.b, x.c
  FROM full_table x
  JOIN (SELECT MAX(t.ratio) AS max_ratio 
          FROM full_table t) y ON y.max_ratio = x.ratio

...is the identical to using: ......与使用相同:

SELECT x.a, x.b, x.c
  FROM (SELECT ...) x
  JOIN (SELECT MAX(t.ratio) AS max_ratio 
          FROM (SELECT ...) t) y ON y.max_ratio = x.ratio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM