【问题标题】:HIVE: Finding running total excluding duplicatesHIVE:查找运行总数,不包括重复项
【发布时间】:2017-09-29 18:20:31
【问题描述】:

您好,我手头有一个非常特殊的问题,我无法找到解决方案。我有一个表 UserViews 具有以下列:

Progdate(String)
UserName(String)

表中的虚拟数据:

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

每次用户查看程序时,表中都会有一个条目。例如,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

以下查询:

选择 Progdate、UniqueUsersByDate、Sum(UniqueUsersByDate) over(Order By Progdate) RunningTotalNewUsers 从 ( 选择 Progdate,count(distinct UserName) UniqueUsersByDate 从 用户视图 按 Progdate 分组 按 Progdate 排序 ) 紫外线;

会给我结果:

Progdate UniqueUsersByDate RunningTotalNewUsers 20161119 3 3 20161120 3 6 20161121 4 10

但我想要的是所有第一次观看该节目的用户的总和。意思是如果用户A在20161119又在20161120观看了节目,那么这个用户的计数不应该在20161120的累计中重复。因此我想要从上表中得到的结果是:

Progdate UniqueUsersByDate RunningTotalNewUsers 20161119 3 3 20161120 3 5 20161121 4 7

我只在 HIVE HQL 中寻找解决方案。非常感谢您对此问题的任何意见。

谢谢。

【问题讨论】:

  • 以非 ISO 格式将日期存储为字符串以及更多内容的任何原因?
  • 数据库已经创建。我对此没有任何发言权:(我必须只使用现有的。顺便说一句,这只是实际表格的缩小版本。实际表格大约有 50 多列。我刚刚发布了相关的一次让它变得简单。

标签: hive cumulative-sum


【解决方案1】:
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                     |
+-------------+--------------------+-----------------------+

附言
理论上,聚合和使用 SUM 分析函数不需要额外的子查询,但解析器似乎存在问题(错误/功能)。
请注意,额外的子查询不一定表示额外的执行阶段,例如select * from (select * from (select * from (select * from (select * from t)t)t)t)t;select * from t 将具有相同的执行计划。

【讨论】:

  • 您设计的查询按预期给出了答案。查询很复杂,我需要一些时间来消化和理解。一些解释将有助于理解。不过,感谢您的帮助。
猜你喜欢
  • 2021-12-27
  • 2015-12-21
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
相关资源
最近更新 更多