我有一个结果集,为简单起见,我将其称为三列的表“选项卡”:类别、子类别和日期,按类别排序,然后按日期排序。该数据集是一个网格,我希望在该网格之上执行其他处理。我的问题是在数据集中唯一标识(或按顺序标记)组。下面的 SQL 是我所追求的(GID1 或 GID2 都可以),基于前 3 列的存在。我尝试了 group_id、grouping_id、rank、dense_rank 并且要么错过了其中一个技巧,要么正在尝试一些非常尴尬的事情。GID 的顺序并不重要,重要的是组号分配基于排序的数据(类别然后日期)。
CREATE TABLE Tab
("Category" varchar2(1), "SubCategory" varchar2(7), "Date" int, "GID1" int, "GID2" int);
INSERT ALL
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'bannana', 20120101, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'grape', 20120102, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120103, 1, 1)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120104, 1, 1)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'bannana', 20120105, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120106, 2, 2)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120107, 2, 2)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'apple', 20120108, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120109, 3, 3)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'apple', 20120101, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'bannana', 20120102, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'apple', 20120103, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'bannana', 20120104, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120105, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120106, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120107, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120108, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120109, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'grape', 20120101, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'grape', 20120102, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120103, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'bannana', 20120104, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'grape', 20120105, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'pear', 20120106, 1, 5)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120107, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120108, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120109, NULL, NULL)
SELECT * FROM dual
;