Impala where 子句中的解析函数

Impala Analytical functions in where clause

所以问题的基本前提是我在 hadoop 中有一些巨大的 tables,我需要每个月从中获取一些样本。我模拟了下面的内容以显示我想要的东西,但显然这不是真实数据...

--Create the table
CREATE TABLE exp_dqss_team.testranking (
  Name STRING,
  Age INT,
  Favourite_Cheese STRING
  ) STORED AS PARQUET;

--Put some data in
INSERT INTO TABLE exp_dqss_team.testranking
VALUES (
  ('Tim', 33, 'Cheddar'),
  ('Martin', 49, 'Gorgonzola'),
  ('Will', 39, 'Brie'),
  ('Bob', 63, 'Cheddar'),
  ('Bill', 35, 'Brie'),
  ('Ben', 42, 'Gorgonzola'),
  ('Duncan', 55, 'Brie'),
  ('Dudley', 28, 'Cheddar'),
  ('Edmund', 27, 'Brie'),
  ('Baldrick', 29, 'Gorgonzola'));

我想得到的是每个奶酪类别中最年轻的2个人。下面给出了每个奶酪类别的年龄排名,但不会将其限制在前两个:

SELECT RANK() OVER(PARTITION BY favourite_cheese ORDER BY age asc) AS rank_my_cheese, favourite_cheese, name, age
FROM exp_dqss_team.testranking;

如果我添加 WHERE 子句,则会出现以下错误:

WHERE clause must not contain analytic expressions

SELECT RANK() OVER(PARTITION BY favourite_cheese ORDER BY age asc) AS rank_my_cheese, favourite_cheese, name, age
FROM exp_dqss_team.testranking
WHERE RANK() OVER(PARTITION BY favourite_cheese ORDER BY age asc) <3;

有没有比创建所有排名的 table,然后在排名中使用 WHERE 子句从中选择更好的方法?

你能试试这个吗?

select * from (
SELECT RANK() OVER(PARTITION BY favourite_cheese ORDER BY age asc) AS rank_my_cheese, favourite_cheese, name, age
FROM exp_dqss_team.testranking
) as temp
where rank_my_cheese <= 2;