2016-10-27 9 views
0

を使用してCに2つのテーブルを結合して、最新のつぶやきを保存私はウィッテンクエリ持っていた3つのテーブルA、B共通のものがtweeetId、そのために私はAに参加したい両方のテーブルにACCOUNT_IDとBですがあったハイブ

Select created_date, tweet_text, user_description 
from A inner join 
    B 
    on A.tweetId = B.tweetId and A.account_id = B.accountid; 

私はBに参加した後に最新のcreated_dateを取得し、その最新の作成日とCの最新の作成日を比較したいと思います。クエリが実行されるたびに目標がCにデータを挿入する必要があります。AとBに存在する最新のつぶやきがCにダンプされる必要があります。

+0

「この最新の作成日とCの最新の作成日を比較してください。最新のデータをCに挿入するだけでなく、既存のデータとマージする必要がありますか? – leftjoin

答えて

0

最新のつぶやきを日付で取得するには、row_number 。

insert overwrite C --or insert into 
select * from 
(
    select created_date, tweet_text, user_description 
      row_number() over(partition by weetId, account_id order by created_date desc) as rn, 
      tweetId, account_id --PK 
    from A inner join 
     B 
     on A.tweetId = B.tweetId and A.account_id = B.accountid 
)s 
where s.rn=1; --take the latest