Apache Impala group by和order by

时间:2018-02-04 10:55:13

标签: sql impala

我是apache impala的新手。致力于构建查询。

这是样本数据。

------------------------------------------------------------------
| id_ingredient | ingredient_name | price |  timestamp | deleted |
------------------------------------------------------------------
|        1      | potatoes        | 10.00 | 1445899655 |    0    |
|        2      | tomatoes        | 20.00 | 1445836421 |    0    |
|        1      | sweet potatoes  | 10.00 | 1445899132 |    0    |
|        1      | sweet potatoes  | 15.00 | 1445959231 |    0    |
|        2      | tomatoes        | 30.00 | 1445894337 |    1    |
|        3      | chicken         | 50.00 | 1445899655 |    0    | 

我想构建一个表格,该表格仅显示尚未删除的每种成分的最新状态。

非常感谢提前

2 个答案:

答案 0 :(得分:0)

你可以使用

select ingredient_name , max(timestamp) 
from my_table
where deleted = 0
group by ingredient_name 

用于检索整行=可以

select * from my_table 
inner join  (
select ingredient_name , max(timestamp) 
from my_table a
where deleted = 0
group by ingredient_name ) t on t.ingredient_name  =a.ingredient_name  
       and t.timestamp = a.timestamp

答案 1 :(得分:0)

如果假设deleted仅在最近的状态,那么你可以这样做:

select t.*
from (select t.*,
             row_number() over (partition by ingredient_name order by timestamp desc) as seqnum
      from t
     ) t
where seqnum = 1 and deleted = 0;