配置单元sql将记录计数添加为列

plupiseo  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(314)

我有类似以下的记录

fruit      day
apple      1/1/1990   
apple      1/2/1990
apple      1/3/1990
plum       1/1/1990
orange     1/1/1990
orange     1/2/1990
orange     1/3/1990

我想保持一个项目为每天假设项目将增加1每天运行总数。例如

fruit      day            count
apple      1/1/1990       1
apple      1/2/1990       2
apple      1/3/1990       3
plum       1/1/1990       1
orange     1/1/1990       1
orange     1/2/1990       2
ulydmbyx

ulydmbyx1#

你可以用开窗的 COUNT :

SELECT *, COUNT(*) OVER(PARTITION BY fruit ORDER BY day)
FROM tab;

dbfiddle演示

u0njafvf

u0njafvf2#

你也可以使用 subquery :

select *,
       (select count(*) from table where fruit  = t.fruit and day <= t.day) count
from table t;

相关问题