我试图通过从另一个表中选择两列来插入表中。我想订购它,首先是第一列手动(青铜,银然后是金),然后是第二列,按升序排列。我找到了一个回答的问题,允许我按青铜/银/金订购排名,或者我可以订购数字栏,但我不能让它同时做两件事。
这就是我用青铜/银/金订购的方式:
-- The Code:
drop table test5;
drop sequence id5_seq;
create table test5
(id_num number primary key,
ranks varchar(20),
numbers number);
create sequence id5_seq
minvalue 1
maxvalue 10000
start with 1
increment by 1
nocycle;
create or replace trigger auto_test5_id
before insert on test5
for each row
begin
:NEW.id_num := id5_seq.nextval;
end;
/
insert into test5(ranks,numbers) (select distinct ranking,numbering from temp1 where ranking is not null) order by case
when ranking = 'Bronze' then 1
when ranking = 'Silver' then 2
when ranking = 'Gold' then 3
else 4
end
;
select * from test5;
-- The Results:
Table TEST5 dropped.
Sequence ID5_SEQ dropped.
Table TEST5 created.
Sequence ID5_SEQ created.
Trigger AUTO_TEST5_ID compiled
14 rows inserted.
ID_NUM RANKS NUMBERS
---------- -------------------- ----------
1 Bronze 32
2 Bronze 80
3 Bronze 16
4 Bronze 160
5 Bronze 8
6 Bronze 190
7 Silver 4
8 Gold 190
9 Gold 94
10 Gold 32
11 Gold 8
12 Gold 80
13 Gold 64
14 Gold 16
14 rows selected
我的目标是让最终结果看起来像这样:
ID_NUM RANKS NUMBERS
---------- -------------------- ----------
1 Bronze 8
2 Bronze 16
3 Bronze 32
4 Bronze 80
5 Bronze 160
6 Bronze 190
7 Silver 4
8 Gold 8
9 Gold 16
10 Gold 32
11 Gold 64
12 Gold 80
13 Gold 94
14 Gold 190
我正在使用SQL开发人员Oracle 11g。 谢谢大家的帮助和时间:)
答案 0 :(得分:0)
添加编号字段,不确定是否使用oracle的插入语法
insert into test5(ranks,numbers)
select distinct ranking,numbering from temp1 where ranking is not null order by (case
when ranking = 'Bronze' then 1
when ranking = 'Silver' then 2
when ranking = 'Gold' then 3
else 4
end)
, numbering -- to sort asc