简体   繁体   English

case语句中的Rank函数

[英]Rank Function inside case statement

I am trying to use Rank Function inside a case statement and give where rank_number = 1 , it's throwing error as unexpected where Condition.我正在尝试在 case 语句中使用 Rank 函数并给出 where rank_number = 1 ,它会在 where Condition 中抛出错误。 Can some one help me how to assign rank in where clause inside case statement有人可以帮我如何在case语句中的where子句中分配等级吗

You can't use the RANK() analytic function (or any other one, for that matter) in the WHERE clause of a query.您不能在查询的WHERE子句中使用RANK()分析函数(或任何其他分析函数)。 The results of the rank computation are not yet available.秩计算的结果尚不可用。 But they are available in the SELECT clause or the ORDER BY clause.但它们在SELECT子句或ORDER BY子句中可用。 One workaround would be to subquery:一种解决方法是子查询:

SELECT *
FROM
(
    SELECT t.*, RANK() OVER (ORDER BY blah) rnk
    FROM yourTable t
) s
WHERE rnk = 1;

Some databases support a QUALIFY clause, where it is possible to use analytic functions.一些数据库支持QUALIFY子句,可以在其中使用分析函数。 Assuming you are using something like Teradata or BigQuery, you could use:假设您使用的是 Teradata 或 BigQuery 之类的东西,您可以使用:

SELECT *
FROM yourTable
WHERE 1 = 1
QUALIFY RANK() OVER (ORDER BY blah) = 1;

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

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