有一张产品生产日期表,字段有批次号,产品名,生产日期。结构以及测试数据如下:
CREATE TABLE "VICEL"."TEST"
( "BATCH" VARCHAR2(255),
"NAME" VARCHAR2(255),
"PRODUCE_DATE" DATE
) ;
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次1', '产品1', TO_DATE('2021-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次1', '产品1', TO_DATE('2021-05-02 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次1', '产品3', TO_DATE('2021-05-03 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次1', '产品3', TO_DATE('2021-05-04 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次1', '产品2', TO_DATE('2021-05-05 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次2', '产品2', TO_DATE('2021-05-06 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次2', '产品2', TO_DATE('2021-05-07 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次2', '产品1', TO_DATE('2021-05-08 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次2', '产品1', TO_DATE('2021-05-09 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次2', '产品3', TO_DATE('2021-05-10 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次3', '产品3', TO_DATE('2021-05-11 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次3', '产品3', TO_DATE('2021-05-12 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次3', '产品2', TO_DATE('2021-05-13 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次3', '产品2', TO_DATE('2021-05-14 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "VICEL"."TEST"("BATCH", "NAME", "PRODUCE_DATE") VALUES ('批次3', '产品1', TO_DATE('2021-05-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
select
-- 序号规则:按批次分组,再按生产日期排序
row_number() over(partition by batch order by produce_date ) rn,
batch,
name,
produce_date
from test;
select
-- 序号规则:按批次分组,再按名称分组,最后按生产日期排序
row_number() over(partition by batch,name order by produce_date ) rn,
batch,
name,
produce_date
from test