简体   繁体   English

在 mysql 中使用 PARTITION BY 的 RANK 查询失败

[英]RANK query using PARTITION BY fails in mysql

I tried below query to partition by but which fails with below query, the inner query works我尝试通过以下查询进行分区,但以下查询失败,内部查询有效

select  issueid, task_type, assignee, timeoriginalestimate, CREATED,
dense_rank() over ( partition by issueid order by CREATED desc ) as rank
       from( 
       --- Complex query with p.pname, i.issuenum, cg.issueid, it.pname task_type, i.assignee, i.timeoriginalestimate, cg.CREATED, columns which works fine
       )

Exception: You have an error in your SQL syntax;例外:您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '( partition by issueid order by CREATED desc ) as rank from( SELECT p.pna' at line 3检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 3 行的“(按 issueid 顺序按创建的 desc 分区)作为排名从(SELECT p.pna')附近

Update:更新:

SELECT VERSION(); -- 5.6.27

Although your MySQL version do not support Window function, I am letting this is not the issue.尽管您的 MySQL 版本不支持 Window function,但我认为这不是问题所在。 Guess you have a higher version and window function is supported.猜你有更高的版本,并且支持 window function。

Now, in your query you have defined the Alias of a column name to "Rank" which is a reserved keyword for your database and you can not use that as column name.现在,在您的查询中,您已将列名的别名定义为“Rank”,这是您的数据库的保留关键字,您不能将其用作列名。

Hope this below hints will help you-希望以下提示对您有所帮助-

select  
issueid, 
task_type, 
assignee, 
timeoriginalestimate, 
CREATED,
dense_rank() over ( partition by issueid order by CREATED desc ) as rn -- change the alias name
from( 
   -- Your subquery
) A -- Also need to give a Alias name to your sub query 

Finally, if you have lower version check this LINK for help to get an idea of creating Row_number or Ranking for MySQL older versions.最后,如果您有较低版本,请查看此链接以获取有关为 MySQL 旧版本创建 Row_number 或 Ranking 的想法。

In addition, this following sample query will really help you finding solution for different type row_number in mysql-此外,以下示例查询将真正帮助您找到 mysql 中不同类型 row_number 的解决方案-

SET @simple_row_number := 0;
SET @id_wise_row_number := 0;
SET @dense_rank_per_id := 0;
SET @prev := 0;

SELECT *,

@simple_row_number := @simple_row_number + 1 AS simple_row_number,
@id_wise_row_number := IF(issueid > @prev, @id_wise_row_number + 1, @id_wise_row_number) AS id_wise_row_number,
@dense_rank_per_id :=IF(issueid > @prev,1, @dense_rank_per_id + 1) AS dense_rank_per_id,

@prev := A.issueid Prev_IssueId

FROM (
    SELECT 1 issueid, '20200601' CREATED UNION ALL
    SELECT 1 issueid, '20200401' CREATED UNION ALL
    SELECT 1 issueid, '20200501' CREATED UNION ALL
    SELECT 1 issueid, '20200201' CREATED UNION ALL
    SELECT 1 issueid, '20200301' CREATED UNION ALL
    SELECT 2 issueid, '20200301' CREATED UNION ALL
    SELECT 2 issueid, '20200201' CREATED UNION ALL
    SELECT 2 issueid, '20200401' CREATED
) A
ORDER BY issueid, CREATED DESC

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

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