SQL Query JOIN with Table

后端 未结 2 452
日久生厌
日久生厌 2020-12-02 02:24
select user_id, prod_and_ts.product_id as product_id, prod_and_ts.timestamps as 
timestamps from testingtable2 LATERAL VIEW explode(purchased_item) exploded_table
as         


        
2条回答
  •  醉酒成梦
    2020-12-02 02:35

    I think you can do what you want with two queries, but I'm not 100% sure. Often in this situation, it is sufficient to find things in the first table that don't match in the second table. You are also trying to get a "closest" match, which is why this is challenging.

    The following query looks for matches on user id and exactly one of the other two fields, and then combines them:

    SELECT table2.buyer_id, table2.item_id, table2.created_time, prod_and_ts.*
    from (select user_id, prod_and_ts.product_id as product_id, prod_and_ts.timestamps as timestamps
          from testingtable2 LATERAL VIEW
               explode(purchased_item) exploded_table as prod_and_ts
         ) prod_and_ts JOIN
         table2
         on prod_and_ts.user_id = table2.buyer_id and
            prod_and_ts.product_id = table2.item_id and
            prod_and_ts.timestamps <> UNIX_TIMESTAMP(table2.created_time)
    union all
    SELECT table2.buyer_id, table2.item_id, table2.created_time, prod_and_ts.*
    from (select user_id, prod_and_ts.product_id as product_id, prod_and_ts.timestamps as timestamps
          from testingtable2 LATERAL VIEW
               explode(purchased_item) exploded_table as prod_and_ts
         ) prod_and_ts JOIN
         table2
         on prod_and_ts.user_id = table2.buyer_id and
            prod_and_ts.product_id <> table2.item_id and
            prod_and_ts.timestamps = UNIX_TIMESTAMP(table2.created_time)
    

    This will not find situations where there is no match on either field.

    Also, I've written this using the "on" syntax rather than "where". I assume HIVE supports this.

提交回复
热议问题