基本上,我有下表:
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()
first_value(Amount) over (partition by ID)
Distinct 也无济于事,因为它返回唯一的行,而不是其值
Distinct
编辑: 选择顺序无关紧要
即使使用Dimitri提到的有问题的组合,这也对我有用。我不知道这对于大音量有多快
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