【问题标题】:SQL analytic functions - How to assign number to a partition?SQL 分析函数 - 如何为分区分配编号?
【发布时间】:2011-09-04 18:44:35
【问题描述】:

我正在使用 Oracle。

假设我有一个包含这个样本的表格,随机的,内容:

columnA     | columnB  | content
--------------------------------
AfBkxZ      | 292      | a
LDglkK      | 181      | b
AfBkxZ      | 51       | c
AfBkxZ      | 315      | d
LDglkK      | 808      | e
Cee89g      | 1        | f

我想有一个视图,其中我对 columnA 中的每个值都有一个唯一编号,并在 columnA 具有该值的行内记录编号。

根据上面的示例数据想要的结果:

Group_number | Record_number | columnB | content
------------------------------------------------
1            | 2             | 292     | a             (1.2)
3            | 1             | 181     | b             (3.1)
1            | 1             | 51      | c             (1.1)
1            | 3             | 315     | d             (1.3)
3            | 2             | 808     | e             (3.2)
2            | 1             | 1       | f             (2.1)

我可以通过row_number() over (partition by columnA order by columnB asc)获取record_number

我如何获得group_number,它实际上是旧columnA 的友好排序别名?

谢谢。

【问题讨论】:

    标签: sql oracle analytic-functions


    【解决方案1】:

    您可以使用dense_rank 为A 列编号:

    dense_rank() over (order by columnA)
    

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多