无法在Oracle中按日期设置行顺序

编码公爵夫人

我有以下sql:

UPDATE TABLE1 SET SORT_COLUMN=ROWNUM WHERE ACTIVE_RECORD='Y'

可以,但是它将顺序设置为当前行的顺序,我需要按日期排序,如下所示:

UPDATE TABLE1 SET SORT_COLUMN=ROWNUM WHERE ACTIVE_RECORD='Y' ORDER BY START_DATE

但这显然是行不通的。如何根据日期更新SORT列?

小脚丫

这是我根据您先前的问题(已删除)写的内容。子查询可能是您要寻找的答案:

SQL> create table test
  2    (start_date date,
  3     sort       number);

Table created.

SQL> insert into test (start_date)
  2    select date '2019-03-04' from dual union all
  3    select date '2019-01-12' from dual union all
  4    select date '2019-12-17' from dual;

3 rows created.

SQL> update test t set
  2    t.sort = (select x.rn
  3              from (select start_date,
  4                           row_number() over (order by start_date) rn
  5                    from test
  6                   ) x
  7              where x.start_date = t.start_date);

3 rows updated.

SQL> select * From test;

START_DATE       SORT
---------- ----------
04.03.2019          2
12.01.2019          1
17.12.2019          3

SQL>

新列ACTIVE_RECORD不会带来多大变化-将其添加到该示例中应该很简单。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章