在 where 子句中使用 Hive tile 结果
Using Hive ntile results in where clause
我想在 Hive 中获取 table 第一个四分位数的摘要数据。下面是获取每个四分位数中最大视图数的查询:
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'
如何在 where
子句或 group by
子句中引用 ntile
结果?
您不能将窗口函数放在 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
;
SQL 中的 WHERE 语句只能在 table 架构中的现有列上 select。为了在计算列上执行该功能,请使用 HAVING 而不是 WHERE。
SELECT name, NTILE(4) OVER (ORDER BY total_views) AS quartile
FROM view_data
HAVING quartile = 1
我想在 Hive 中获取 table 第一个四分位数的摘要数据。下面是获取每个四分位数中最大视图数的查询:
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'
如何在 where
子句或 group by
子句中引用 ntile
结果?
您不能将窗口函数放在 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
;
SQL 中的 WHERE 语句只能在 table 架构中的现有列上 select。为了在计算列上执行该功能,请使用 HAVING 而不是 WHERE。
SELECT name, NTILE(4) OVER (ORDER BY total_views) AS quartile
FROM view_data
HAVING quartile = 1