oracle数据库,表名字为test,字段只有一个name,sql查询时添加一个id,如何得到下图的效果,谢谢啦
1个回答
展开全部
创建表插入数据:
create table test
(name varchar2(10),
id int);
insert into test (name) values ('张三');
insert into test (name) values ('张三');
insert into test (name) values ('张三');
insert into test (name) values ('李四');
insert into test (name) values ('李四');
insert into test (name) values ('王二');
insert into test (name) values ('王二');
commit;
执行update语句:
update test set test.id=(select s.rn from
(select name,row_number() over (order by rn) rn
from
(select name,rownum rn from test order by rownum) t
where rn in (select min(rn) from (select name,rownum rn from test order by rownum) t group by name)) s
where test.name=s.name);
执行后结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询