如何从Oracle SQL中的多个列中选择唯一值?

基本上,我有下表:

ID | Amount
AA | 10
AA | 20
BB | 30
BB | 40
CC | 10
CC | 50
DD | 20
DD | 60
EE | 30
EE | 70

我需要在每个列中获取唯一条目,如下例所示:

ID | Amount
AA | 10
BB | 30
CC | 50
DD | 60
EE | 70

到目前为止,以下代码片段几乎提供了我想要的内容,但first_value()可能返回一些值,这在当前列中不是唯一的:

first_value(Amount) over (partition by ID)

Distinct也没有帮助,因为它返回唯一的行,而不是它的值

编辑:
选择顺序无关紧要

最佳答案 即使是迪米特里提到的有问题的组合,这对我也有用.我不知道大卷的速度有多快

with ids as (
  select id, row_number() over (order by id) as rn
  from data
  group by id
), amounts as (
  select amount, row_number() over (order by amount) as rn
  from data
  group by amount
)
select i.id, a.amount
from ids i
  join amounts a on i.rn = a.rn;

SQLFiddle目前对我不起作用,这是我的测试脚本:

create table data (id varchar(10), amount integer);

insert into data values ('AA',10);
insert into data values ('AA',20);
insert into data values ('BB',30);
insert into data values ('BB',40);
insert into data values ('CC',10);
insert into data values ('CC',50);
insert into data values ('DD',20);
insert into data values ('DD',60);
insert into data values ('EE',30);
insert into data values ('EE',70);

输出:

id | amount
---+-------
AA |     10
BB |     20
CC |     30
DD |     40
EE |     50
点赞