我想获取Hive中表格的第一个四分位数的摘要数据。以下是获取每个四分位数中的最大视图数的查询:
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
结果?
答案 0 :(得分:6)
您不能在window子句中放置窗口函数,因为如果存在复合谓词,它会产生歧义。所以使用子查询。
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
;
答案 1 :(得分:-1)
SQL中的WHERE语句只能在表模式中的现有列上进行选择。要在计算列上执行该功能,请使用 HAVING 而不是WHERE。
SELECT name, NTILE(4) OVER (ORDER BY total_views) AS quartile
FROM view_data
HAVING quartile = 1