如何逐列比较两行?

问题描述 投票:0回答:1

我有两个接口,用于解析数据并将转换后的数据放入名为 rbc_swift_parser 的表中。 一个接口(旧)是用 C 编写的,另一个是用 Java 编写的。 Java 仍处于测试环境中。我有一个名为 host_reference 的主键。单个交易在表中将有两行,一行由 Java 接口解析,另一行由 C 接口解析。我可以比较单笔交易的每一列(总列为 103 列)吗?简而言之,我的查询将显示哪些列彼此不匹配。我尝试使用分区,但它不起作用。 询问:

select * from rbc_swift_parser where deal_number = '11NOV2400000018'

上面的查询将显示两行。我对数据库具有只读访问权限。 我正在使用的查询:

SELECT 
    a.host_reference,
    a.deal_number,
    CASE WHEN a.column1 <> b.column1 THEN 'Mismatch' ELSE 'Match' END AS column1_comparison,
    CASE WHEN a.column2 <> b.column2 THEN 'Mismatch' ELSE 'Match' END AS column2_comparison,
    -- Repeat for all 103 columns
    CASE WHEN a.column103 <> b.column103 THEN 'Mismatch' ELSE 'Match' END AS column103_comparison
FROM 
    rbc_swift_parser a
JOIN 
    rbc_swift_parser b
ON 
    a.deal_number = b.deal_number
    AND a.host_reference <> b.host_reference
WHERE 
    a.deal_number = '11NOV2400000018';
oracle-database oracle-sqldeveloper
1个回答
0
投票

不太确定你想做什么 - 你的查询应该给你不匹配的结果。为所有这些列编写的代码太多了。

--      S a m p l e    D a t a :
Create Table rbc_swift_parser ( host_reference   Number(6) Primary Key,
                                deal_number      Number(6),
                                column1          Varchar2(32),
                                column2          Varchar2(32),
                                column3          Varchar2(32),
                                --
                                column103        Varchar2(32)    );
Insert Into rbc_swift_parser
Select 1, 101, 'col_1 A', 'col_2 B', 'col_3 C', 'col_103 Z' From Dual Union All 
Select 2, 101, 'col_1 A', 'col_2 X', 'col_3 C', 'col_103 Z' From Dual Union All 
--
Select 3, 222, 'col_1 Y', 'col_2 B', 'col_3 C', 'col_103 Z' From Dual Union All 
Select 4, 222, 'col_1 A', 'col_2 B', 'col_3 C', 'col_103 Z' From Dual Union All 
--
Select 5, 303, 'col_1 A', 'col_2 B', 'col_3 C', 'col_103 Z' From Dual Union All 
Select 6, 303, 'col_1 A', 'col_2 B', 'col_3 C', 'col_103 Z' From Dual;
--      Y o u r    S Q L    c o d e :
SELECT 
    a.host_reference,
    a.deal_number,
    CASE WHEN a.column1 <> b.column1 THEN 'Mismatch' ELSE 'Match' END AS column1_comparison,
    CASE WHEN a.column2 <> b.column2 THEN 'Mismatch' ELSE 'Match' END AS column2_comparison,
    -- Repeat for all 103 columns
    CASE WHEN a.column103 <> b.column103 THEN 'Mismatch' ELSE 'Match' END AS column103_comparison
FROM 
    rbc_swift_parser a
JOIN 
    rbc_swift_parser b
ON 
    a.deal_number = b.deal_number
    AND a.host_reference <> b.host_reference
WHERE 
    a.deal_number = '101';
    
HOST_REFERENCE DEAL_NUMBER COLUMN1_ COLUMN2_ COLUMN103
-------------- ----------- -------- -------- ---------
             2         101 Match    Mismatch Match   
             1         101 Match    Mismatch Match  

也许您可以考虑创建一个 PLSQL 过程或函数来检查不匹配的数据...
...如下所示...

SET SERVEROUTPUT ON
Declare
    Cursor t IS 
       Select Distinct DEAL_NUMBER From RBC_SWIFT_PARSER;
    Cursor c IS
        Select    COLUMN_NAME
        From      all_tab_columns 
        Where     OWNER = 'RAKICD' And TABLE_NAME = 'RBC_SWIFT_PARSER' And COLUMN_ID > 2;
    v_col       VarChar2(32);
    v_deal_num  Number(6) := 101; 
    v_sql       VarChar2(4000);
    v_result    VarChar2(4000) := 'Missmached: ';
Begin
    Open t;
        Loop
            Fetch t Into v_deal_num;
            Exit When t%NOTFOUND;
            Open c;
                LOOP
                    Fetch c Into v_col;
                    Exit When c%NOTFOUND;
                    v_sql := 'Select Distinct CASE WHEN a.' || v_col || ' != ' || 'b.' || v_col || ' ' || 
                                       'THEN ' || '''' || v_col || '''' || ' ' || 
                                  'ELSE  ''OK'' END as col_name ' || 
                           'From RBC_SWIFT_PARSER a ' ||
                           'Inner Join RBC_SWIFT_PARSER b ON(a.DEAL_NUMBER = b.DEAL_NUMBER And b.HOST_REFERENCE != a.HOST_REFERENCE) ' || 
                           'Where a.DEAL_NUMBER = ' || v_deal_num;
                  Execute Immediate v_sql Into v_col;
                  If v_col != 'OK' Then 
                      v_result := v_result || Chr(10) || Chr(9) || v_col || ' For Deal ' || v_deal_num;
                  End If;                  
                END LOOP;
            Close c;
        End Loop;
    Close t;
    dbms_output.put_line(v_result); 
End;
/
--      R e s u l t : 
Missmached: 
    COLUMN2 For Deal 101
    COLUMN1 For Deal 222
© www.soinside.com 2019 - 2024. All rights reserved.