Hive中不存在其他帐户时,选择一个帐户

时间:2019-05-08 15:31:16

标签: sql hive conditional hiveql where-clause

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

我用async createNotificationListeners() { this.notificationListener = firebase.notifications().onNotification((notification) => { const { title, body } = notification; const localNotification = new firebase.notifications.Notification({ sound: 'default', show_in_foreground: true, }) firebase.notifications().displayNotification(localNotification) .catch(err => console.error(err)); }); 过滤了记录,但是所有记录都来了

Account_type = 'Second'

我的预期结果是:

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

实际结果是

Account_ID  Account_type

102         Second

enter image description here

1 个答案:

答案 0 :(得分:0)

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

例如此分析功能

max(case when Account_type='First'  then 1 else 0 end) over(partition by account_id) first_exists_flag
对于数据集中的所有1记录,

将为account_id=101,对于所有其他记录,将为0。希望你有主意。

当“第一”不存在时选择“第二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;

在“第一”和“第二”不存在时选择“第三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

进行过滤