【问题标题】:Select one account when other accounts do not exist in Hive当 Hive 中不存在其他帐户时选择一个帐户
【发布时间】:2019-09-26 09:26:46
【问题描述】:

我想在 Hive 中不存在其他 account_type 时选择帐户

我用Account_type = 'Second'过滤记录,但所有记录都来了

Select Account_ID, Account_type
From Account
Where Account_type = 'Second'

我的预期结果是:

Account_ID  Account_type

102         Second

实际结果是

Account_ID  Account_type

101         Second 
102         Second

【问题讨论】:

    标签: sql hive conditional hiveql where-clause


    【解决方案1】:

    如果Account_ID 存在一些account_type,则使用解析函数计算标志。

    例如这个解析函数

    max(case when Account_type='First'  then 1 else 0 end) over(partition by account_id) first_exists_flag
    

    对于数据集中的所有 account_id=101 记录和 0 将是 1。希望你明白了。

    当 First 不存在时选择 Second account_type:

    with account as (
        select a.*, 
                  max(case when Account_type='First'  then 1 else 0 end) over(partition by account_id) first_exists_flag,
                  max(case when Account_type='Second' then 1 else 0 end) over(partition by account_id) second_exists_flag
                  --you can add more such flags
              from Account a
    )
    
    select account_id, account_type
      from account a
    where a.account_type='Second' and first_exists_flag=0;
    

    当 First 和 Second 不存在时选择 Third account_type:

    with account as (
        select a.*, 
                  max(case when Account_type='First'  then 1 else 0 end) over(partition by account_id) first_exists_flag,
                  max(case when Account_type='Second' then 1 else 0 end) over(partition by account_id) second_exists_flag
                  --you can add more such flags
              from Account a
    )
    
    select account_id, account_type
      from account a
    where a.account_type='Third' and first_exists_flag=0 and second_exists_flag=0;
    

    您也可以在存在第三个帐户类型以外的其他帐户时计算标志:

    max(case when Account_type <> 'Third'  then 1 else 0 end) over(partition by account_id) other_account_exist_flag
    

    并使用other_account_exist_flag=0过滤

    【讨论】:

    • 我的数据库是 abc。运行此查询时,我收到以下错误消息。 ParseException 第 1:13 行不匹配输入“。”期望语句中的“ABC”附近有 AS(状态 = 42000,代码 = 40000)
    • with abc.account as (select a.*, max(case when Account_type='First' then 1 else 0 end) over(partition by account_id) first_exists_flag, max(case when Account_type='Second ' then 1 else 0 end) over(partition by account_id) second_exists_flag -- 你可以从 abc.Account a 添加更多这样的标志) select account_id, account_type from abc.account a where a.account_type='Second' and first_exists_flag=0;
    • with account as (select a.*, max(case when Account_type='First' then 1 else 0 end) over(partition by account_id) first_exists_flag, max(case when Account_type='Second' then 1 else 0 end) over(partition by account_id) second_exists_flag -- 你可以从 abc.Account 添加更多这样的标志 a) select account_id, account_type from account a where a.account_type='Second' and first_exists_flag=0;
    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2017-05-16
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    相关资源
    最近更新 更多