【问题标题】:Why can't I use dense_rank for SQL 'rank scores'?为什么我不能将 dense_rank 用于 SQL 的“排名分数”?
【发布时间】:2018-09-25 10:10:15
【问题描述】:

我正在使用 SQL 中的 dense_rank 函数来解决 leetcode 'rank scores' 问题(https://leetcode.com/problems/rank-scores/description/):

select Score, dense_rank() over (order by Score) Rank
from Scores
order by Score desc

它总是给我以下错误:

Line 2: SyntaxError: near '(order by Score) Rank
from Scores
order by Score desc'

我想知道如何使这个答案正确?非常感谢!

另外,我意识到大多数人使用答案而不使用 DENSE_RANK 函数,这很令人困惑,因为对我来说 DENSE_RANK 可能是解决问题的最直观的方法。有人知道吗?再次感谢!

【问题讨论】:

  • 并非所有数据库都支持dense_rank()。大概,你没有。
  • @GordonLinoff 我正在使用 Mysql。如果最近没有更新,它不支持任何窗口功能。
  • MySQL 8.0 版本支持包括DENSE_RANK() 在内的窗口函数,但该版本目前仍处于预发布阶段。 dev.mysql.com/doc/refman/8.0/en/…
  • Line 2: SyntaxError: near '(order by Score) Rank from Scores order by Score desc' 在我看来不像是 MySQL 错误

标签: mysql window-functions dense-rank


【解决方案1】:

修改答案

My SQL 不支持dense_rank,但它支持其他窗口函数。 查看this 答案寻求帮助。

或者,您可以在另一个 SQL 服务器中运行代码

【讨论】:

  • 。 .在 8.0 版本之前,MySQL 完全不支持 no 窗口函数。
  • MySQL 8 确实支持dense_rank: dev.mysql.com/doc/refman/8.0/en/… 以及其他窗口函数。
  • 虽然,即使在 emulate DENSE_RANK 功能
【解决方案2】:

较新版本的 MySQL 支持 rank()dense_rank() 等 Windows 函数。

更改您的 SQL 查询,即 as "Rank"as Rank 传递 leetcode 测试用例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2020-03-04
    • 2013-07-12
    • 1970-01-01
    • 2013-06-02
    • 2011-12-14
    相关资源
    最近更新 更多