BigQuery中树结构的深度

时间:2015-10-23 22:26:49

标签: sql google-bigquery

我的数据如下所示:

| child_id | parent_id |
|    10    |    Null   |
|    13    |     10    |
|    15    |     13    |
|    11    |     10    |
|    16    |     11    |
|    19    |     15    |

这可以看作是一棵树。我现在想确定每个child_id的深度。所以例子应该是:

| child_id | parent_id |  depth  |
|    10    |    Null   |    0    |
|    13    |     10    |    1    |
|    15    |     13    |    2    |
|    11    |     10    |    1    |
|    16    |     11    |    2    |
|    19    |     15    |    3    |

我想在BigQuery中解决这个问题;我不确定如何,因为我不认为一个人可以轻松地使用递归。也许以某种方式将它传递给UDF可能是一种合理的方法。

1 个答案:

答案 0 :(得分:1)

我刚刚发布了一个查询,使用刚刚发布的“关于BigQuery的全黑客新闻数据集”。

这里的问题是一些评论指向父故事,而其他评论发布到父评论,并通过它们搜索原始故事很难(因为这是一个递归操作)。解决了:

  SELECT p0.id, s.id, s.title, level
  FROM (
    SELECT p0.id, p0.parent, p2.id, p3.id, p4.id, COALESCE(p7.parent, p6.parent, p5.parent, p4.parent, p3.parent, p2.parent, p1.parent, p0.parent) story_id,
           GREATEST(IF(p7.parent IS null, -1, 7), IF(p6.parent IS null, -1, 6), IF(p5.parent IS null, -1, 5), IF(p4.parent IS null, -1, 4), IF(p3.parent IS null, -1, 3),
                    IF(p2.parent IS null, -1, 2), IF(p1.parent IS null, -1, 1), 0) level
    FROM    [fh-bigquery:hackernews.comments] p0
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p1 ON p1.id=p0.parent
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p2 ON p2.id=p1.parent
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p3 ON p3.id=p2.parent
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p4 ON p4.id=p3.parent
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p5 ON p5.id=p4.parent
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p6 ON p6.id=p5.parent
    LEFT JOIN EACH [fh-bigquery:hackernews.comments] p7 ON p7.id=p6.parent
    HAVING level=0
    LIMIT 100
  ) a
  LEFT JOIN EACH [fh-bigquery:hackernews.stories] s
  ON s.id=a.story_id

(有这么多左连接消耗了大量资源,所以要大量运行它我会寻找不同的策略)

https://news.ycombinator.com/item?id=10440502