2017-06-23 48 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; 

エラー:

が 'NULL' の近くに '参加'

でEOFの欠落を次のように

表構造がある

答えて

1

wherefrom句に続き、selectにはwhereの1つしかないため、構文上問題があります。

私はちょうど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

おかげで、すべての3つの円の交点にデータを取得するため、これは、クエリを記述するための正しい方法です。 select count(*) from(c.customer cust cから) グループby c.customer 合計の場合(1,2,3,4,5,6,7,8,9 )then 1 else 0 end)> 0 )c – user3447653

+1

@ user3447653。 。 。はい、それはうまくいくでしょう。 –

関連する問題