0

我正在运行一个 netezza sql 进程作为 shell 脚本的一部分,并且在其中一个 sql 代码中,如果来自 2 个不同表的行数不匹配,我希望它引发错误或异常。

SQL 代码:

/*  The following 2 tables should return the same number of rows to make sure the process is correct */

select              count(*) 
from                (
                select distinct col1, col2,col3 
                from table_a
                where  week > 0 and rec >= 1
                ) as x ;


select              count(*) 
from                (
                select distinct col1, col2, col3
                from table_b
                ) as y ;

如何比较 2 行计数并在 netezza SQL 进程中引发异常/错误,以便在 2 行计数不相等时退出进程?

4

2 回答 2

1

我猜这里最好的解决方案是在脚本中进行。
即将count(*) 的结果存储在变量中,然后比较它们。nzsql 具有仅返回单个查询的结果数据的命令行选项。

如果它必须用普通的 SQL 来完成,那么一个可怕的、可怕的组合就是使用除以零。它很难看,但我以前在测试东西时使用过它。我的头顶上:

with 
subq_x as select count(*) c1 .... ,
subq_y as select count(*) c2 ...
select (case when (subq_x.c1 != subq_y.c1) then 1/0 else 1 end) counts_match;

我有没有提到这很丑陋?

于 2013-11-20T00:02:29.333 回答
1

我同意脚本是最好的选择。但是,您仍然可以通过使用交叉连接来检查您的 SQL 本身

Select a.*
from Next_Step_table a cross join
(select case when y.y_cnt is null then 'No Match' else 'Match' end as match
from (select count(*) as x_cnt
from  ( select distinct col1, col2,col3 
        from table_a
        where  week > 0 and rec >= 1
       )) x left outer join
(select count(*) as y_cnt
from  (select distinct col1, col2, col3
       from table_b
       )) y  on x.x_cnt=y.y_cnt) match_tbl
where match_tbl.match='Match'
于 2013-12-03T12:19:33.780 回答