如何使用 SQLite 按日期查找层次结构中的级别

How to find levels in a hierarchy by dates using SQLite

问题

如何使用带有递归表达式的 SQLite 按日期为层次结构中的每个人找到级别。

详情

我发现以下 post detailing the use of recursive queries for doing computation on hierarchical data in SQLite and that one on Whosebug 用于 SQLite3 上的基本递归查询。

现在,有一个很好的detailed example on the documentation of SQLite关于如何从单个员工的角度处理分层数据的计算(第 3.2 部分的示例)。

我做了什么?

好吧,到目前为止我知道如何计算选定个人的水平(哇哦),但我无法弥合差距并使此查询按日期应用于所有个人.

这是我为 1 个人完成的部分查询:

WITH RECURSIVE supervisor_of(id, boss_id, date_interest) AS (
        SELECT org_1.id, org_1.boss_id, org_1.date_interest
        FROM org org_1
        WHERE id = 4 -- Here is the input for the individual
    UNION
        SELECT org_1.id, org_1.boss_id, org_1.date_interest
        FROM org org_1
        JOIN supervisor_of so
            ON so.boss_id = org_1.id
            AND so.date_interest = org_1.date_interest
)
SELECT *,
    COUNT(id) AS level
FROM supervisor_of
GROUP BY date_interest
ORDER BY date_interest

并且输出:

| id   | boss_id | date_interest | level |
| ---- | ------- | ------------- | ----- |
| 4    | 2       | 2             | 3     |
| 4    | 2       | 3             | 3     |

但我无法绕过我的脑袋得到这个结果:

| id   | boss_id | date_interest | level |
| ---- | ------- | ------------- | ----- |
| 1    |         | 1             | 1     |
| 2    | 1       | 1             | 2     |
| 3    | 1       | 1             | 2     |
| 1    |         | 2             | 1     |
| 2    | 1       | 2             | 2     |
| 3    | 1       | 2             | 2     |
| 4    | 2       | 2             | 3     |
| 1    |         | 3             | 1     |
| 2    | 1       | 3             | 2     |
| 3    | 1       | 3             | 2     |
| 4    | 2       | 3             | 3     |
| 5    | 4       | 3             | 4     |

以下是如何加载进行此测试的数据:

CREATE TABLE org(
  id TEXT,
  boss_id TEXT,
  date_interest TEXT
);
-- 1st Date
INSERT INTO org (id, boss_id, date_interest) VALUES(1, NULL, 1);
INSERT INTO org (id, boss_id, date_interest) VALUES(2, 1, 1);
INSERT INTO org (id, boss_id, date_interest) VALUES(3, 1, 1);
-- 2nd Date
INSERT INTO org (id, boss_id, date_interest) VALUES(1, NULL, 2);
INSERT INTO org (id, boss_id, date_interest) VALUES(2, 1, 2);
INSERT INTO org (id, boss_id, date_interest) VALUES(3, 1, 2);
INSERT INTO org (id, boss_id, date_interest) VALUES(4, 2, 2);
-- 3rd Date
INSERT INTO org (id, boss_id, date_interest) VALUES(1, NULL, 3);
INSERT INTO org (id, boss_id, date_interest) VALUES(2, 1, 3);
INSERT INTO org (id, boss_id, date_interest) VALUES(3, 1, 3);
INSERT INTO org (id, boss_id, date_interest) VALUES(4, 2, 3);
INSERT INTO org (id, boss_id, date_interest) VALUES(5, 4, 3);

从代码中删除 WHERE 子句,以便查询所有 id 和递归 CTE select [=14= 的第二部分] 而不是 org_1.id.
最后你还必须按 id 分组:

WITH RECURSIVE supervisor_of(id, boss_id, date_interest) AS (
  SELECT org_1.id, org_1.boss_id, org_1.date_interest
  FROM org org_1
  UNION
  SELECT so.id, org_1.boss_id, org_1.date_interest
  FROM org org_1 JOIN supervisor_of so
  ON so.boss_id = org_1.id AND so.date_interest = org_1.date_interest
)
SELECT *, COUNT(*) AS level
FROM supervisor_of
GROUP BY date_interest, id
ORDER BY date_interest, id

参见demo
结果:

id boss_id date_interest level
1 null 1 1
2 1 1 2
3 1 1 2
1 null 2 1
2 1 2 2
3 1 2 2
4 2 2 3
1 null 3 1
2 1 3 2
3 1 3 2
4 2 3 3
5 4 3 4