2016-09-22 6 views
0

私はmysqlで作業していますが、ハイブにいくつかのクエリを複製する必要があります。ハイブ:顧客がトランザクションから一緒に購入したアイテムを見つける

私は、以下の情報取得したい

transaction table

このフォームでテーブルを持っています。mysqlで

Resultant table

を、以下のクエリが動作します

SELECT c.original_item_id, c.bought_with_item_id, count(*) as times_bought_together 
FROM (
    SELECT a.item_id as original_item_id, b.item_id as bought_with_item_id 
    FROM items a 
    INNER join items b 
    ON a.transaction_id = b.transaction_id AND a.item_id != b.item_id where original_item_id in ('B','C')) c 
GROUP BY c.original_item_id, c.bought_with_item_id; 

しかし、私はそうではありませんこれをハイブクエリに変換することができますが、私は多くのシャッフル結合を試して、どこで条件を置き換えても、必要な結果が得られていません。これについて助けを見つけることができれば素晴らしいだろう

答えて

0

ハイブは等しくない結合をサポートしていません。撮影した

create table items(transaction_id smallint, item_id string); 

insert overwrite table items 
select 1 , 'A' from default.dual union all 
select 1 , 'B' from default.dual union all 
select 1 , 'C' from default.dual union all 
select 2 , 'B' from default.dual union all 
select 2 , 'A' from default.dual union all 
select 3 , 'A' from default.dual union all 
select 4 , 'B' from default.dual union all 
select 4 , 'C' from default.dual; 

SELECT c.original_item_id, c.bought_with_item_id, count(*) as times_bought_together 
FROM (
     SELECT a.item_id as original_item_id, b.item_id as bought_with_item_id 
     FROM items a 
     INNER join items b ON a.transaction_id = b.transaction_id 
     WHERE 
      a.item_id in ('B','C') --original_item_id 
     and a.item_id != b.item_id 
    ) c 
GROUP BY c.original_item_id, c.bought_with_item_id; 
--- 
OK 
original_item_id  bought_with_item_id  times_bought_together 
B  A  2 
B  C  2 
C  A  1 
C  B  2 

は時間:24.164秒、フェッチ:4行(複数可)

しかし、あなたは where句の中に、この条件 a.item_id != b.item_idを移動することができます
関連する問題