HIVE:找到 运行 总数,不包括重复项
HIVE: Finding running total excluding duplicates
您好,我手头有一个非常特殊的问题,我无法找到解决方案。我有一个 table UserViews 具有以下列:
Progdate(String)
UserName(String)
table 中的虚拟数据:
Progdate UserName
20161119 A
20161119 B
20161119 C
20161119 B
20161120 D
20161120 E
20161120 A
20161121 B
20161121 A
20161121 B
20161121 F
20161121 G
用户每次观看节目时,table 中都会有一个条目。例如11月19日,用户A看了一次节目,所以有一个条目。用户 B 看了两次节目,因此该用户在 11 月 19 日有两个条目,依此类推。
Select Progdate, count(distinct UserName) UniqueUsersByDate
from UserViews
group by Progdate;
上面的查询会给我所有看过该节目的唯一用户的日期计数
Progdate UniqueUsersByDate
20161119 3
20161120 3
20161121 4
以下查询:
Select Progdate, UniqueUsersByDate, Sum(UniqueUsersByDate) over(Order By Progdate) RunningTotalNewUsers
from
(
Select Progdate, count(distinct UserName) UniqueUsersByDate
from
UserViews
group by Progdate SORT BY Progdate
) UV;
会给我结果:
Progdate UniqueUsersByDate RunningTotalNewUsers
20161119 3 3
20161120 3 6
20161121 4 10
但是我想要的是运行所有第一次观看该节目的用户总数。意味着如果用户 A 在 20161119 上观看了节目,然后在 20161120 上再次观看了节目,那么这个用户的计数不应在 20161120 的 运行 总数中重复。因此我想要从上面得到的结果 table 是:
Progdate UniqueUsersByDate RunningTotalNewUsers
20161119 3 3
20161120 3 5
20161121 4 7
我只在 HIVE HQL 中寻找解决方案。非常感谢任何对此问题的投入。
谢谢。
select Progdate
,UniqueUsersByDate
,sum(Users1stOcc) over
(
order by Progdate
) as RunningTotalNewUsers
from (select Progdate
,count (distinct UserName) as UniqueUsersByDate
,count (case when rn = 1 then 1 end) as Users1stOcc
from (select Progdate
,UserName
,row_number() over
(
partition by UserName
order by Progdate
) as rn
from UserViews
) uv
group by Progdate
) uv
;
+-------------+--------------------+-----------------------+
| progdate | uniqueusersbydate | runningtotalnewusers |
+-------------+--------------------+-----------------------+
| 2016-11-19 | 3 | 3 |
| 2016-11-20 | 3 | 5 |
| 2016-11-21 | 4 | 7 |
+-------------+--------------------+-----------------------+
P.s.
理论上聚合和使用SUM解析函数不需要额外的子查询,但是解析器好像有问题(bug/feature)
请注意,额外的子查询不一定表示额外的执行阶段,例如select * from (select * from (select * from (select * from (select * from t)t)t)t)t;
和 select * from t
将有相同的执行计划。
您好,我手头有一个非常特殊的问题,我无法找到解决方案。我有一个 table UserViews 具有以下列:
Progdate(String)
UserName(String)
table 中的虚拟数据:
Progdate UserName
20161119 A
20161119 B
20161119 C
20161119 B
20161120 D
20161120 E
20161120 A
20161121 B
20161121 A
20161121 B
20161121 F
20161121 G
用户每次观看节目时,table 中都会有一个条目。例如11月19日,用户A看了一次节目,所以有一个条目。用户 B 看了两次节目,因此该用户在 11 月 19 日有两个条目,依此类推。
Select Progdate, count(distinct UserName) UniqueUsersByDate
from UserViews
group by Progdate;
上面的查询会给我所有看过该节目的唯一用户的日期计数
Progdate UniqueUsersByDate 20161119 3 20161120 3 20161121 4
以下查询:
Select Progdate, UniqueUsersByDate, Sum(UniqueUsersByDate) over(Order By Progdate) RunningTotalNewUsers from ( Select Progdate, count(distinct UserName) UniqueUsersByDate from UserViews group by Progdate SORT BY Progdate ) UV;
会给我结果:
Progdate UniqueUsersByDate RunningTotalNewUsers 20161119 3 3 20161120 3 6 20161121 4 10
但是我想要的是运行所有第一次观看该节目的用户总数。意味着如果用户 A 在 20161119 上观看了节目,然后在 20161120 上再次观看了节目,那么这个用户的计数不应在 20161120 的 运行 总数中重复。因此我想要从上面得到的结果 table 是:
Progdate UniqueUsersByDate RunningTotalNewUsers 20161119 3 3 20161120 3 5 20161121 4 7
我只在 HIVE HQL 中寻找解决方案。非常感谢任何对此问题的投入。
谢谢。
select Progdate
,UniqueUsersByDate
,sum(Users1stOcc) over
(
order by Progdate
) as RunningTotalNewUsers
from (select Progdate
,count (distinct UserName) as UniqueUsersByDate
,count (case when rn = 1 then 1 end) as Users1stOcc
from (select Progdate
,UserName
,row_number() over
(
partition by UserName
order by Progdate
) as rn
from UserViews
) uv
group by Progdate
) uv
;
+-------------+--------------------+-----------------------+
| progdate | uniqueusersbydate | runningtotalnewusers |
+-------------+--------------------+-----------------------+
| 2016-11-19 | 3 | 3 |
| 2016-11-20 | 3 | 5 |
| 2016-11-21 | 4 | 7 |
+-------------+--------------------+-----------------------+
P.s.
理论上聚合和使用SUM解析函数不需要额外的子查询,但是解析器好像有问题(bug/feature)
请注意,额外的子查询不一定表示额外的执行阶段,例如select * from (select * from (select * from (select * from (select * from t)t)t)t)t;
和 select * from t
将有相同的执行计划。