作为Shell脚本的一部分,我正在运行Netezza SQL进程,并且在其中一个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行计数不相等,则将其退出该过程?

有帮助吗?

解决方案

我同意脚本是最好的选择。但是,您仍然可以使用十字架加入来进行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'

其他提示

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

如果必须用普通的SQL完成,那么将有效的可怕,可怕的kludge是使用零分割。这很丑陋,但是我在测试内容时使用过它。我的头顶:

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;

我是否提到这很丑?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top