【问题标题】:Different ascending values depending on other attribute in table in select不同的升序值取决于选择表中的其他属性
【发布时间】:2016-03-11 06:37:34
【问题描述】:

我想要下表(如果相关,它来自连接)

 id  | atr | order (actually date and time)
----------------
  3  | foo  | 4
  6  | foo  | 3
  2  | bar  | 1
  9  | bar  | 2

变成这样

 id  | atr  | order | suborder
-------------------------------
  6  | foo  |   3   |   1
  3  | foo  |   4   |   2
  2  | bar  |   1   |   1
  9  | bar  |   2   |   2

SELECT 用于 VIEW。

我得到的最接近的是使用 rownum 的子列中的 1,2,3,4。但是我还没有想出用另一个属性来分隔“索引”的方法。

这是怎么做到的?

【问题讨论】:

  • 你的 SQL 是什么样的?

标签: sql oracle select view


【解决方案1】:

使用窗口函数ROW_NUMBER:

SELECT id,atr ,"order",
       ROW_NUMBER() OVER(PARTITION BY atr ORDER BY "order") AS suborder
FROM table_name;

LiveDemo

order 是关键字,需要引用。

【讨论】:

  • 谢谢小伙子!这解决了我的问题,现在来了解我刚刚做了什么。顺便说一句,我没想到它是一个关键字,下次我会在这里的问题中使用更好的命名。无论如何,这些列名都没有与我的 sql 中的相同。再次感谢!
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 2023-01-10
  • 1970-01-01
  • 2021-10-14
相关资源
最近更新 更多