Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle SQL联接查询结果_Sql_Oracle - Fatal编程技术网

Oracle SQL联接查询结果

Oracle SQL联接查询结果,sql,oracle,Sql,Oracle,我有下面两张桌子 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: T

我有下面两张桌子

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
我们有一个从源表获取数据并加载到目标表的进程。但在这里,流程有一条规则,即目标中的主行应该具有源表中的源名称,其中源ID='Monster'。 在这里,Target中的以下条目是正确的

RECORDID    AI CL ID    Source Name Op Code
123 111 Dice.com    Secondary
456 111 ABC COMPANY Primary
但以下是错误的,主要来源名称是Dice.com,应该是ABC公司

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

因此,我需要一个查询,它可以识别目标中存在相同问题的所有行

为什么AI_CL_ID=111的两行是正确的?根据您的规范,它们是错误的,因为recorded=123对应于“Monster”,但在您的
target\u系统中有“Secondary”

要查找
target_system
表中所有
op_code
错误的行,可以使用以下查询。假设:对(id、代码)是唯一的
target_系统
;任何列中都没有空值;
target\u system
中的
source\u name
始终正确(当与
id
code
匹配时,它与
source\u system
中的
source\u name
匹配);标记
'Primary'
是特殊的,但是除了
'Secondary'
之外,可能还有其他标记

解决方案不包括
target_system
定义后从“with”到“closing”)”的行;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