Select 一个账户,当其他账户在 Hive 中不存在时
Select one account when other accounts do not exist in Hive
当 Hive
中不存在其他 account_type 帐户时,我想 select
我用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
如果 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
记录将是 1
,对于数据集中的所有其他记录将是 0
。希望你明白了。
Select 第二个 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;
Select 第三个 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;
当除第三account_type以外的其他存在时,您也可以计算标志:
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
进行过滤
当 Hive
中不存在其他 account_type 帐户时,我想 select我用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
如果 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
记录将是 1
,对于数据集中的所有其他记录将是 0
。希望你明白了。
Select 第二个 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;
Select 第三个 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;
当除第三account_type以外的其他存在时,您也可以计算标志:
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