我正在尝试对两个表进行计数比较。由于减号运算符在 hive 中不起作用,因此它没有发生。您能否提供一些简单的方法来对两个表进行计数比较。
select 'Call Detail - Hive T1 to HDFS Staging - Data Compare',
case when cnt>0 then 'Fail' Else 'Pass' end
from
(select count(*) cnt from (
(select
count(*) from students1 s1)-
(select count(*) from students2 s2)
) as tbl1
) as tbl2;
抛出错误:
FAILED: ParseException line 81:0 cannot recognize input near '(' '(' 'select' in from source
最佳答案
如果您没有按列分组,请使用cross join
。在这种情况下,它将产生包含两个计数的一行:
select s.cnt-s1.cnt diff, case when abs(s.cnt-s1.cnt) > 0 then 'Fail' Else 'Pass' end result
from
(select count(*) cnt from students1 s1) s
cross join
(select count(*) cnt from students2 s2) s1
如果您要添加一些按列分组以比较更详细的粒度,则对按列分组使用FULL JOIN
:
select s.col1 s_col1, s1.col1 s1_col1, s.cnt-s1.cnt diff, case when abs(s.cnt-s1.cnt) > 0 then 'Fail' Else 'Pass' end result
from
(select count(*) cnt, col1 from students1 s1 group by col1) s
full join
(select count(*) cnt, col1 from students2 s2 group by col1) s1
on s.col1 = s1.col1
此查询将从两个表中返回具有计算差异的连接行和未连接的行。
关于sql - 比较两个配置单元表之间的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51835743/