比较两个配置单元表之间的计数

时间:2018-08-14 07:18:17

标签: sql hadoop hive hdfs

我正在尝试计算两个表之间的比较。由于减号运算符在蜂巢中不起作用,因此不会发生。您能否提供一些简单的方法来进行两个表之间的计数比较。

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;

抛出错误:

  

失败:ParseException行81:0无法识别源中'(''(''select'附近的输入

2 个答案:

答案 0 :(得分:3)

如果没有按列分组,请使用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

此查询将返回计算出差异的联接行,并且两个表中都不联接行。

答案 1 :(得分:1)

检查以下查询..在我的系统上本地工作正常。 让我知道是否有帮助。

select 'Call Detail - Hive T1 to HDFS Staging - Data Compare',
       case 
       when (sum(cnt1) - sum(cnt2)) > 0 
       then 'PASS' 
       else 'FAIL' 
       end as count_records
  from (select count(*) as cnt1, 0 as cnt2 from students1 
        union all
        select 0 as cnt1, count(*) as cnt2 from students1  ) tbl;