三个共享相同主键的表之间的完全连接

时间:2014-05-05 22:13:13

标签: sql join hive

我想加入三个表来检查每个组的共享和非共享用户的数量。 理想情况下,我想生产类似的东西。

Image borrowed from www.mathcentral.uregina.ca

我有一个大表,可存储许多类的数据,包括W,D和P. 为了评估用户是否参加了多个课程,我将为每个组创建一个子查询,并使用full join合并这些结果表。我的问题是我必须在同一个主键(userpid)上加入所有三个表。 我试图以这种方式编写查询但是从Hive收到一条错误消息,说IS NULL运算符只接受一个参数。

select
        isnull(iq1.userpid, iq2.userpid) userpid,
        groupW,
        groupD,
        sum( case when iq2.userpid is not null then 1 else 0 end ) groupP


from
        ( select
                isnull(q1.userpid, q2.userpid) userpid,
                ( case when q1.userpid is not null then 1 else 0 end ) groupW,
                ( case when q1.userpid is not null then 1 else 0 end ) groupD

        from
                -- users who attended class W
                ( select distinct userpid
                from store.attendance
                where classid = 1165
                  and datehour between '2014-04-28 00:00:00' and '2014-04-30 00:00:00'
                 ) q1

                full outer join

                -- users who attended class D
                ( select distinct userpid
                from store.attendance
                where classid= 1174
                  and datehour between '2014-04-28 00:00:00' and '2014-04-30 00:00:00'
                 ) q2

                  on q1.userpid = q2.userpid ) iq1

                full outer join

                -- users who attended class P
                ( select distinct userpid
                from store.attendance
                where classid = 1173
                  and datehour between '2014-04-28 00:00:00' and '2014-04-30 00:00:00'
                 ) iq2

                  on iq2.userpid = iq1.userpid

;

是否有其他函数或方法来编写查询,我可以使用它来实现相同的目标? 然后,我可以看到按类分享非共享用户的数量,使用一系列case when调用,或者在R或Python中处理它。

1 个答案:

答案 0 :(得分:1)

我建议使用两层聚合来做这件事。首先为每个类创建标志。然后通过这些标志聚合:

select has_1165, has_1174, has_1173, count(*) as cnt, min(userpid), max(userpid)
from (select userpid,
             max(case when classid = 1165 then 1 else 0 end) as has_1165,
             max(case when classid = 1174 then 1 else 0 end) as has_1174,
             max(case when classid = 1173 then 1 else 0 end) as has_1173
      from store.attendance
      where classid in (1165, 1173, 1174) and
            datehour between '2014-04-28 00:00:00' and '2014-04-30 00:00:00'
      group by userpid
     ) a
group by has_1165, has_1174, has_1173;