我想获取Hive中表的第一个四分位数的摘要数据。下面是一个查询,以获取每个四分位数中的最大 View 数:

SELECT NTILE(4) OVER (ORDER BY total_views) AS quartile, MAX(total_views)
FROM view_data
GROUP BY quartile
ORDER BY quartile;

此查询将获取第一个四分位数中所有人员的姓名:
SELECT name, NTILE(4) OVER (ORDER BY total_views) AS quartile
FROM view_data
WHERE quartile = 1

我在两个查询中都收到此错误:
Invalid table alias or column reference 'quartile'

如何在ntile子句或where子句中引用group by结果?

最佳答案

您不能将windowing函数放在where子句中,因为如果存在复合谓词,它将创建歧义。因此,请使用子查询。

select quartile, max(total_views) from
(SELECT total_views, NTILE(4) OVER (ORDER BY total_views) AS quartile,
FROM view_data) t
GROUP BY quartile
ORDER BY quartile
;


select * from
(SELECT name, NTILE(4) OVER (ORDER BY total_views) AS quartile
FROM view_data) t
WHERE quartile = 1
;

关于hadoop - 使用Hive ntile导致where子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31540469/

10-12 17:47