我是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 |
我想构建一个表格,该表格仅显示尚未删除的每种成分的最新状态。
非常感谢提前
答案 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;