我正在尝试提供最好的HiveQL查询来获取行列表,其中一列将具有该节点具有的(直接)子节点数。数据库是分层的,所以它看起来像这样:
| ID | Some other column | ParentID |
+-----------------------------------+
| 1 | XXXXXXXXXX x X X | NULL |
| 2 | XXXXXXXXXX x X X | 1 |
| 3 | XXXXXXXXXX x X X | 2 |
| 4 | XXXXXXXXXX x X X | 1 |
我试图查询输出这样的内容:
| ID | Some other column | child count |
+--------------------------------------+
| 1 | XXXXXXXXXX x X X | 2 |
| 2 | XXXXXXXXXX x X X | 1 |
| 3 | XXXXXXXXXX x X X | 0 |
| 4 | XXXXXXXXXX x X X | 0 |
答案 0 :(得分:0)
使用LEFT JOIN
尝试类似的内容。
SELECT a.id,
COALESCE (b.child_count, 0) "child count"
FROM mytable a
LEFT JOIN (SELECT parentid,
Count(*) child_count
FROM mytable
GROUP BY parentid) b
ON a.id = b.parentid;