递归分层 – Google BigQuery

huangapple go评论66阅读模式
英文:

Recursive Hierarchical - Google BigQuery

问题

输入

递归分层 – Google BigQuery

输出

递归分层 – Google BigQuery

注意:存在循环引用,必须在第一个“节点”处停止 - 注意:停止。
就像示例输出中一样。

  • 有些情况下没有“节点”子级,这些用Flag = 2标记,注意:结束;

  • 有些情况下“节点”父级也是子级。

  • 对于每个条目ID,我们希望应用相同的逻辑。在这个输出示例中,使用了案例1010。

  • 预期的输出可以是表格、列表等。

谢谢。

英文:

Input

递归分层 – Google BigQuery

Output

递归分层 – Google BigQuery

Note: There are circular references and these must be stopped at the first "node" - Note: Stop.
As in the example output.

-There are cases where there are no "nodes" children, these are marked with Flag = 2, Note: End;

-There are cases where the "node" Father is also a child.

-For each entry ID, we want to apply this same logic. In the example of this output, case 1010 was used.

-The expected output can be a table, list, etc.

Tks.

答案1

得分: 0

以下是您要翻译的代码部分:

WITH RECURSIVE sample_table AS (
  -- put your *input* except *flag* here
), 
find_ends AS (
  SELECT DISTINCT t.*, IF(s.id IS NULL, true, false) AS ended
    FROM sample_table t
    LEFT JOIN sample_table s ON t.parent_id = s.id
),
iterations AS (
  SELECT id, parent_id, [id, parent_id] ids, id = parent_id duplicated, ended
    FROM find_ends WHERE id = '1010'
   UNION ALL
  SELECT t.id, t.parent_id,
         r.ids || [t.parent_id],
         t.parent_id IN UNNEST(r.ids),
         t.ended
    FROM iterations r JOIN find_ends t ON r.parent_id = t.id 
   WHERE duplicated IS FALSE AND r.ended IS FALSE
)
SELECT ids FROM iterations WHERE duplicated OR ended ORDER BY ARRAY_LENGTH(ids);

查询结果

+---------------------------------------------+
|                     ids                     |
+---------------------------------------------+
| "[1010,3021]"                               |
| "[1010,3020]"                               |
| "[1010,1012,2021]"                          |
| "[1010,1012,2020]"                          |
| "[1010,1012,1013,1012]"                     |
| "[1010,1012,1013,1018,5020]"                |
| "[1010,1012,1013,1018,1015,6020]"           |
| "[1010,1012,1013,1018,1020,1020]"           |
| "[1010,1012,1013,1018,1015,6021]"           |
| "[1010,1012,1013,1018,1016,4022]"           |
| "[1010,1012,1013,1018,1015,1018]"           |
| "[1010,1012,1013,1018,1016,1018]"           |
| "[1010,1012,1013,1018,1016,1010]"           |
| "[1010,1012,1013,1018,1015,1016,1018]"      |
| "[1010,1012,1013,1018,1015,1020,1020]"      |
| "[1010,1012,1013,1018,1015,1016,1010]"      |
| "[1010,1012,1013,1018,1020,1015,1020]"      |
| "[1010,1012,1013,1018,1020,1015,1018]"      |
| "[1010,1012,1013,1018,1015,1016,4022]"      |
| "[1010,1012,1013,1018,1020,1015,6020]"      |
| "[1010,1012,1013,1018,1020,1015,6021]"      |
| "[1010,1012,1013,1018,1015,1020,1015]"      |
| "[1010,1012,1013,1018,1020,1015,1016,1018]" |
| "[1010,1012,1013,1018,1020,1015,1016,4022]" |
| "[1010,1012,1013,1018,1020,1015,1016,1010]" |
+---------------------------------------------+

如果您需要更多的帮助,请随时告诉我。

英文:

You can try below recursive approach.

WITH RECURSIVE sample_table AS (
  -- put your *input* except *flag* here
), 
find_ends AS (
  SELECT DISTINCT t.*, IF(s.id IS NULL, true, false) AS ended
    FROM sample_table t
    LEFT JOIN sample_table s ON t.parent_id = s.id
),
iterations AS (
  SELECT id, parent_id, [id, parent_id] ids, id = parent_id duplicated, ended
    FROM find_ends WHERE id = '1010'
   UNION ALL
  SELECT t.id, t.parent_id,
         r.ids || [t.parent_id],
         t.parent_id IN UNNEST(r.ids),
         t.ended
    FROM iterations r JOIN find_ends t ON r.parent_id = t.id 
   WHERE duplicated IS FALSE AND r.ended IS FALSE
)
SELECT ids FROM iterations WHERE duplicated OR ended ORDER BY ARRAY_LENGTH(ids);

Query results

+---------------------------------------------+
|                     ids                     |
+---------------------------------------------+
| "[1010,3021]"                               |
| "[1010,3020]"                               |
| "[1010,1012,2021]"                          |
| "[1010,1012,2020]"                          |
| "[1010,1012,1013,1012]"                     |
| "[1010,1012,1013,1018,5020]"                |
| "[1010,1012,1013,1018,1015,6020]"           |
| "[1010,1012,1013,1018,1020,1020]"           |
| "[1010,1012,1013,1018,1015,6021]"           |
| "[1010,1012,1013,1018,1016,4022]"           |
| "[1010,1012,1013,1018,1015,1018]"           |
| "[1010,1012,1013,1018,1016,1018]"           |
| "[1010,1012,1013,1018,1016,1010]"           |
| "[1010,1012,1013,1018,1015,1016,1018]"      |
| "[1010,1012,1013,1018,1015,1020,1020]"      |
| "[1010,1012,1013,1018,1015,1016,1010]"      |
| "[1010,1012,1013,1018,1020,1015,1020]"      |
| "[1010,1012,1013,1018,1020,1015,1018]"      |
| "[1010,1012,1013,1018,1015,1016,4022]"      |
| "[1010,1012,1013,1018,1020,1015,6020]"      |
| "[1010,1012,1013,1018,1020,1015,6021]"      |
| "[1010,1012,1013,1018,1015,1020,1015]"      |
| "[1010,1012,1013,1018,1020,1015,1016,1018]" |
| "[1010,1012,1013,1018,1020,1015,1016,4022]" |
| "[1010,1012,1013,1018,1020,1015,1016,1010]" |
+---------------------------------------------+

huangapple
  • 本文由 发表于 2023年3月7日 07:12:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定