문제

나는 쉘 스크립트의 일부로 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'

다른 팁

여기서 가장 좋은 해결책은 대본에서 수행하는 것입니다.
즉, 카운트 (*)의 결과를 변수에 저장 한 다음 비교하십시오. 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