简体   繁体   English

根据Hive中2个源表中的一些规则更新目标中的“标志”

[英]Update the “flag” in target based on some rules from 2 source tables in Hive

I want to update my "flag" column in my target table with "Y" and "N" based on rules and data present in the source table. 我想根据源表中存在的规则和数据,用“ Y”和“ N”更新目标表中的“标志”列。

Rule 1 - If mood is "sad" in source table 1 then update "flag" as "N" 规则1-如果源表1中的情绪为“悲伤”,则将“标志”更新为“ N”

Rule 2- For all the ids and names present in source table 2 update the "flag" as "N". 规则2-对于源表2中存在的所有ID和名称,将“标志”更新为“ N”。

Rule 3- If "dept" fields contains value as "rty" then also flag should be "N". 规则3-如果“部门”字段包含的值为“ rty”,则标志也应为“ N”。 This scenario is not there in screenshot but please answer this as well. 屏幕快照中没有这种情况,但也请回答。

For all remaining fields the flag should be "Y". 对于所有其余字段,标记应为“ Y”。

Please find the attached screenshot for clear understanding. 请找到随附的屏幕截图,以便清楚地了解。

截图

Thanks. 谢谢。

Try below using case when 尝试以下情况时

    SELECT a.id,
       a.dept,
       a.mood,
       b.name,
       CASE
           WHEN a.mood='sad' THEN 'N'
           WHEN a.dept='ytr' then 'N'
           WHEN b.id IS NULL THEN 'N'
           ELSE 'Y'
       END AS flag
FROM sourcetable1 a
LEFT JOIN sourcetable2 b ON a.id=cast(b.id as int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM