我有以下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列?
答案 0 :(得分:2)
这是我根据您先前的问题(已删除)写的内容。子查询可能是您正在寻找的答案:
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
并不会带来多大变化-将其添加到该示例中应该很简单。