2016-10-04 3 views
-2

결과 가입 I가 다음과 같은 두 개의 테이블오라클 SQL 쿼리가

Table 1: SOURCE_SYSTEM 
ID CODE Source ID Source Name 
123 111 Monster Dice.com 
456 111 Dice ABC COMPANY 
456 888 Ticv A2 systems 
4566 999 MOnster hgtt solutions 
789 222 Monster ABC COMPANY 
985 222 Dice Dice.com 

Table 2: TARGET_SYSTEM 
RECORDID AI CL ID Source Name Op Code 
123 111 Dice.com Secondary 
456 111 ABC COMPANY Primary 
789 222 ABC COMPANY Secondary 
985 222 Dice.com Primary 

우리는 목표 테이블에 소스 테이블로드에서 데이터를 가져 실행하는 프로세스를 가지고있다. 그러나 여기에는 프로세스의 기본 행에 대상의 소스 이름이 있어야하며 Source ID = 'Monster'가 있어야한다는 규칙이 있습니다. 여기 대상에서 다음과 같은 항목이

RECORDID AI CL ID Source Name Op Code 
123 111 Dice.com Secondary 
456 111 ABC COMPANY Primary 

정확하지만 다음과 같은 잘못, 기본 소스 이름을 가진 것은 ABC 회사해야 Dice.com입니다.

RECORDID AI CL ID Source Name Op Code 
789 222 ABC COMPANY Secondary 
985 222 Dice.com Primary 

그래서 같은 문제가있는 모든 행을 식별 할 수있는 쿼리가 필요합니다.

+1

귀하의 질문에 명확하지 않으며 다른 독자들도 같은 방식으로 생각할 수 있습니다. 달성하고자하는 것을 더 잘 설명하십시오. – Elyasin

답변

1

왜 AI_CL_ID = 111에 대한 두 행이 맞습니까? 기록 된 = 123은 '괴물'에 해당하지만 귀하의 target_system 테이블에는 '2 차'가 있으므로 사양에 따라 잘못되었습니다.

target_system 테이블의 모든 행이 잘못된 op_code 인 경우 다음 쿼리를 사용할 수 있습니다. 가정 : 쌍 (id, code)은 유일하다 target_system; 어떤 열에도 NULL이 없습니다. source_nametarget_system은 항상 정확합니다 (일치하는 경우 idcode 일 때 source_namesource_system과 일치 함). 마커 'Primary'은 특별하지만 'Secondary' 외에 다른 마커가있을 수 있습니다.

target_system의 정의 이후 솔루션에 "with"에서 "close"까지의 행이 포함되지 않습니다. WITH 절은 쿼리 자체 내에서 테스트 데이터를 생성하는 데 사용되지만 실제로는 select t.id, ...으로 시작하고 기본 테이블이나 뷰를 히트해야합니다.

with 
    source_system (id, code, source_id, source_name) as (
     select 123, 111, 'Monster', 'Dice.com'  from dual union all 
     select 456, 111, 'Dice' , 'ABC COMPANY' from dual union all 
     select 456, 888, 'Ticv' , 'A2 systems'  from dual union all 
     select 4566, 999, 'MOnster', 'hgtt solutions' from dual union all 
     select 789, 222, 'Monster', 'ABC COMPANY' from dual union all 
     select 985, 222, 'Dice' , 'Dice.com'  from dual 
    ), 
    target_system (recordid, ai_cl_id, source_name, op_code) AS (
     select 123, 111, 'Dice.com' , 'Secondary' from dual union all 
     select 456, 111, 'ABC COMPANY', 'Primary' from dual union all 
     select 789, 222, 'ABC COMPANY', 'Secondary' from dual union all 
     select 985, 222, 'Dice.com' , 'Primary' from dual 
    ) 
select t.recordid, t.ai_cl_id, t.source_name, t.op_code 
from target_system t inner join source_system s 
         on t.recordid = s.id and t.ai_cl_id = s.code 
where (s.source_id = 'Monster' and t.op_code != 'Primary') 
     or 
     (s.source_id != 'Monster' and t.op_code = 'Primary') 
order by ai_cl_id, recordid 
; 

출력 (사용자의 입력에, 나는 설명 된대로 출력이 당신이 게시물에 무슨 이후 게시물이 다른은 잘못된 것입니다).

RECORDID AI_CL_ID SOURCE_NAME OP_CODE 
---------- ---------- ----------- --------- 
     123  111 Dice.com Secondary 
     456  111 ABC COMPANY Primary 
     789  222 ABC COMPANY Secondary 
     985  222 Dice.com Primary