hadoop 如何使用子查询或CTE将合并查询转换为配置单元插入操作?

hfwmuf9z  于 2023-08-03  发布在  Hadoop
关注(0)|答案(1)|浏览(158)

我有这个merge语句,但是我不想在hive和impala中使用这个merge语句,是通过使用insert和使用任何类型子查询和CTE来创建相同逻辑的任何其他方法。

MERGE INTO member_staging AS x
    USING (SELECT member_id, first_name, last_name, rank FROM members) y
    ON (x.member_id  = y.member_id)
    WHEN MATCHED AND (
        x.first_name <> y.first_name OR 
        x.last_name <> y.last_name OR 
        x.rank <> y.rank
    )
    THEN
        UPDATE SET x.first_name = y.first_name, 
                   x.last_name  = y.last_name, 
                   x.rank       = y.rank
    WHEN NOT MATCHED THEN
        INSERT VALUES (y.member_id, y.first_name, y.last_name, y.rank);

字符串

42fyovps

42fyovps1#

您可以这样使用updateinsert

-- Update existing records
UPDATE member_staging
SET first_name = y.first_name,
    last_name = y.last_name,
    rank = y.rank
FROM member_staging x
JOIN members y ON x.member_id = y.member_id
WHERE x.first_name <> y.first_name
    OR x.last_name <> y.last_name
    OR x.rank <> y.rank;

-- Insert new records
INSERT INTO TABLE member_staging
SELECT y.member_id, y.first_name, y.last_name, y.rank
FROM members y
LEFT JOIN member_staging x ON x.member_id = y.member_id
WHERE x.member_id IS NULL;

字符串

相关问题