Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 共享同一主键的三个表之间的完全联接_Sql_Join_Hive - Fatal编程技术网

Sql 共享同一主键的三个表之间的完全联接

Sql 共享同一主键的三个表之间的完全联接,sql,join,hive,Sql,Join,Hive,我想加入三个表来检查每个组中共享和非共享用户的数量。 理想情况下,我想生产类似的产品 我有一个大表,其中存储了许多类的数据,包括W、D和p。 为了评估一个用户是否参加了多个课程,我将为每个组创建一个子查询,并使用完全联接合并这些结果表。我的问题是,我必须在同一主键(userpid)上联接所有三个表。 我试图用这种方式编写查询,但从配置单元收到一条错误消息,说为NULL运算符只接受一个参数 select isnull(iq1.userpid, iq2.userpid) userp

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

我有一个大表,其中存储了许多类的数据,包括W、D和p。 为了评估一个用户是否参加了多个课程,我将为每个组创建一个子查询,并使用
完全联接
合并这些结果表。我的问题是,我必须在同一主键(
userpid
)上联接所有三个表。 我试图用这种方式编写查询,但从配置单元收到一条错误消息,说
为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

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

我建议使用两层聚合。首先为每个类创建标志。然后根据这些标志进行聚合:

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;