我在Hive
中有一个查询,如下所示。该查询对我来说工作正常。
select
case
when account_number = '4122268949' then 'Operating'
when account_number = '4127343176' then 'Reserve'
when account_number = '4125604850' then 'sFBo'
when account_number = '2651133000' then 'IDEAL'
when account_number = '4496851031' then 'PwB'
end as account_name,
account_number,
count(distinct(file_name)) as file_name_count,
count(*) as total_file_count,
case when count(*) >=1 then 'Minimum_expected_files_processed'
else 'Files_not_received_today'
end as Files_validation
from
database.table
where
account_number in (
'4122268949',
'4127343176',
'4125604850',
'2651133000',
'4496851031'
)
group by
account_number) tb1;
问题:
如果在表中我没有4496851031
和4127343176
帐号的记录。
然后我希望结果具有
----------------------------------------------------------------------------------------------------
|account_name| account_number| file_name_count| total_file_count| Files_validation|
----------------------------------------------------------------------------------------------------
| Operating | 4122268949| 2 | 20 | Minimum_expected_files_processed |
| Reserve | 4127343176| 0 | 0 | Files_not_received_today |
| sFBo | 4125604850| 3 | 22 | Minimum_expected_files_processed |
| IDEAL | 2651133000| 1 | 1 | Minimum_expected_files_processed |
| PwB | 4496851031| 0 | 0 | Files_not_received_today |
----------------------------------------------------------------------------------------------------
编辑
当我过滤database.table
以添加仅返回3
帐号的多余where子句时,我无法实现我想要的
sample query
select accounts.name as account_name,
accounts.number as account_number,
count(distinct b.file_name) as file_name_count,
count(b.file_name) as total_file_count,
case when count(b.file_name) >= 1 then 'Minimum_expected_files_processed'
else 'Files_not_received_today'
end as Files_validation
from accounts
left join db.table b on b.account_number = accounts.number
where b.load_date >= '2019-08-20'
group by accounts.number, accounts.name
答案 0 :(得分:3)
您应该创建一个包含帐号和名称的表(我们将其称为accounts
)。然后,您可以LEFT JOIN
到另一个表中以获得所需的结果:
select accounts.name as account_name,
accounts.number as account_number,
count(distinct table.file_name) as file_name_count,
count(table.file_name) as total_file_count,
case when count(table.file_name) >= 1 then 'Minimum_expected_files_processed'
else 'Files_not_received_today'
end as Files_validation
from accounts
left join `table` on table.account_number = accounts.number
group by accounts.number, accounts.name
如果您真的不想创建帐户表,则可以使用帐户数据的派生表重写查询:
select accounts.name as account_name,
accounts.number as account_number,
count(distinct table.file_name) as file_name_count,
count(table.file_name) as total_file_count,
case when count(table.file_name) >= 1 then 'Minimum_expected_files_processed'
else 'Files_not_received_today'
end as Files_validation
from (select '4122268949' as number, 'Operating' as name
union
select '4127343176' as number, 'Reserve' as name
union
select '4125604850' as number, 'sFBo' as name
union
select '2651133000' as number, 'IDEAL' as name
union
select '4496851031' as number, 'PwB' as name) accounts
left join `table` on table.account_number = accounts.number
group by accounts.number, accounts.name