【问题标题】:The SQL Number Sequencing [closed]SQL编号排序[关闭]
【发布时间】:2022-07-12 01:17:43
【问题描述】:

假设..

姓名等级 山姆 4
6
9
ROM 5 9

如何获取数据以便在 SQL 中按顺序获取数据。

名称等级序列 山姆 4 1
6 2
9 3
ROM 5 1 9 2

【问题讨论】:

  • 你的 DBMS 是什么? Oracle 和 MySQL 是不同的产品...顺便说一句,您需要的是 ROW_NUMBER() 函数。

标签: mysql sql oracle


【解决方案1】:

现在,我们通常使用分析函数,例如

SQL> with test (name, rank) as
  2    (select 'Sam', 4 from dual union all
  3     select 'Sam', 6 from dual union all
  4     select 'Sam', 9 from dual union all
  5     select 'rom', 5 from dual union all
  6     select 'rom', 9 from dual
  7    )
  8  select name, rank,
  9    row_number() over (partition by name order by rank) sequence
 10  from test;

NAM       RANK   SEQUENCE
--- ---------- ----------
Sam          4          1
Sam          6          2
Sam          9          3
rom          5          1
rom          9          2

SQL>

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    相关资源
    最近更新 更多