[英]Hive : inner join to update "table a" from "table b" where values in "table b" is different from "table a" ? create a last view (a+b)
我在 Hive 中有兩個表(表 a 和表 b)。
表 a 是一個初始化表(完整的)。 表 b 是具有最新更新的增量表(該表具有相同的列和 5 個以上的列,包括用於最后更新的時間戳列)。
我想從兩個表中創建最后一個視圖(完整 + 增量之間的連接)以進行最后更新。
1-我做的第一步是從表 b (delta) 中選擇具有 max(timestamp) 的行以進行最后更新
2-然后我在內部連接中像子查詢一樣使用它:
SELECT full.*, delta.*
FROM table a FULL
INNER JOIN (SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY timestamp
DESC) as rn
FROM b) delta
ON f.id = d.id and d.rn = 1
問題是我必須指定我想在選擇中使用的列,並且在更新表 b 時我將有具有不同值的重復列。
當表 b 中的值與表 a 中的值不同時,我需要一個條件來始終從表 a 中選擇列並用表 b 中的列替換表 a 中的列。
請問有什么解決辦法嗎?
似乎您正在尋找COALESCE或NVL函數。 在這種情況下,您的查詢可能如下所示:
SELECT
A.Person_id as Person_id, -- Person_id is taken from A table
A.Training_id as Training_id, -- Training_id is taken from A table
coalesce(d.Status, A.Status) as Status, -- If Status from Delta table is not null it is taken otherwise Status from A table is taken
coalesce(d.Name, A.Name) as Name -- If Name from Delta table is not null it is taken otherwise Name from A table is taken
FROM A
LEFT JOIN (SELECT -- Left outer join to don't filter out records without updates
B.Status,
B.Name,
ROW_NUMBER() OVER (PARTITION BY Person_id ORDER BY timestamp DESC) as rn
FROM B) d ON (A.Person_id = d.Person_id and d.rn = 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.