oracle 根据其他列值对列值进行排序

rvpgvaaj  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(106)

我需要的SEQ列被安排的零件和BOM组合的基础上。如果BOM值是相同的零件值SEQ值应该是相同的,没有重复的序列,否则递增序列1为不同的零件BOM组合。
零件和物料清单的样本数据以及SEQ的预期输出如下所示:

PART    BOM     SEQ
Part1   BOM1    1
Part1   BOM1    1
Part1   BOM2    2
Part1   BOM2    2
Part1   BOM3    3
Part1   BOM4    4
Part2   BOM12   1
Part2   BOM13   2
Part2   BOM14   3
Part2   BOM15   4
Part2   BOM16   5
Part2   BOM17   6
Part3   BOM14   1
Part4   BOM14   1
Part5   BOM14   1
Part6   BOM14   1
Part7   BOM14   1

字符串

yb3bgrhw

yb3bgrhw1#

dense_rank是你想要的。
样本数据:

SQL> with temp (part, bom) as
  2    (select 'part1', 'bom1' from dual union all
  3     select 'part1', 'bom1' from dual union all
  4     select 'part1', 'bom2' from dual union all
  5     select 'part1', 'bom2' from dual union all
  6     select 'part1', 'bom3' from dual union all
  7     select 'part1', 'bom4' from dual union all
  8     --
  9     select 'part2', 'bom12' from dual union all
 10     select 'part2', 'bom13' from dual union all
 11     select 'part2', 'bom14' from dual union all
 12     select 'part2', 'bom15' from dual union all
 13     select 'part2', 'bom16' from dual union all
 14     select 'part2', 'bom17' from dual union all
 15     --
 16     select 'part3', 'bom14' from dual union all
 17     --
 18     select 'part4', 'bom14' from dual
 19    )

字符串
查询:

20  select part, bom,
 21    dense_rank() over (partition by part order by bom) seq
 22  from temp;

PART  BOM          SEQ
----- ----- ----------
part1 bom1           1
part1 bom1           1
part1 bom2           2
part1 bom2           2
part1 bom3           3
part1 bom4           4
part2 bom12          1
part2 bom13          2
part2 bom14          3
part2 bom15          4
part2 bom16          5
part2 bom17          6
part3 bom14          1
part4 bom14          1

14 rows selected.

SQL>

相关问题