2017-06-23 133 views
0

我试图获得阴影区域的不同人数。3个表格的左外连接

表结构如下:

customer  key 
    A234   1 
    A345   4 
    A12    5 
    A989   6 

enter image description here

HIVE查询:

select count(distinct(a.customer)) 
    from (
     select * 
     from cust 
     where key in (1,2,3)) c 
    left outer join (
     select * 
     from cust 
     where key in (4,5)) a on a.customer= c.customer where c.customer is null 
     join 
      (select * 
      from cust 
      where key in (6,7,8,9)) d on c.customer = d.customer and d.customer is null; 

错误:

在缺少EOF '加入' 接近 '零'

回答

1

由于where遵循from子句,并且给定的select只有一个where,所以存在语法问题。我想用group byhaving。为了让客户:

select c.customer 
from cust c 
group by c.customer 
having sum(case when key in (1, 2, 3) then 1 else 0 end) > 0 and 
     sum(case when key in (4, 5, 6, 7, 8, 9) then 1 else 0 end) = 0; 

然后,您可以使用子查询数得过来:

select count(*) 
from (select c.customer 
     from cust c 
     group by c.customer 
     having sum(case when key in (1, 2, 3) then 1 else 0 end) > 0 and 
      sum(case when key in (4, 5, 6, 7, 8, 9) then 1 else 0 end) = 0 
    ) c 
+0

感谢,在所有三个圆的交叉获取数据,这是编写查询的正确方法: 选择总数(*) (选择c customer from cust c group by c.customer having sum(case case when key in(1,2,3,4,5,6,7,8,9 )then 1 else 0 end)> 0 )c – user3447653

+1

@ user3447653。 。 。是的,那会起作用。 –