2017-06-23 102 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。 。 。是的,那會起作用。 –