我有以下表和数据,我想将 loc1 和 loc2 合并在一列中,并从 loc 列中删除重复值,然后根据 group_no 列对它进行分组,并根据 group_val 升序进行排序
drop table test;
create table test (loc1 number(9), loc2 number(9), group_no number(9),group_val number(9));
insert into test values(2,3,1,90);
insert into test values(2,9,1,10);
insert into test values(4,3,1,70);
insert into test values(6,8,2,20);
insert into test values(11,7,2,80);
insert into test values(20,15,2,70);
insert into test values(15,14,2,30);
insert into test values(21,31,3,50);
insert into test values(31,32,3,40);
预期结果是:
loc group_no
2 1
3 1
4 1
9 1
11 2
7 2
20 2
15 2
6 2
8 2
21 3
31 3
32 3
这段来自 Grish 但没有 group_val 的代码,现在我添加它
select t.loc, max(t.group_no)
(
select loc1 as loc, group_no from test
union
select loc2 as loc, group_no from test
) t
group by t.loc
order by 2,1
if can do it using dense_rank() over partition by group_val.
loc 列将从上到下排序。根据 group_val 列
regards