简体   繁体   English

迁移后用于分析数据的SQL

[英]SQL to analyze data after migration

I have a lot of data migrated and now wants to check them for correctness. 我迁移了大量数据,现在想检查它们的正确性。

Below is a simple example: I have migrated a table which was already filled in another. 下面是一个简单的例子:我已经迁移了一个已经填充了另一个的表。 Now I want to check the number of both tables and compare. 现在我想检查两个表的数量并进行比较。 In addition I have created the following query: 另外我创建了以下查询:

SELECT 'Prüfung1' AS "Prüfung", (CASE 
    WHEN (SELECT count(*) FROM TableA) = (SELECT count(*) FROM TableB)
    THEN 'OK!' 
    ELSE '!!! Anzahl stimmt nicht überein !!!' END) "Ergebnis" FROM dual

However, I would carry out several such tests, and show as one result. 但是,我会进行几次这样的测试,并显示为一个结果。 If I will be replacements in a second test this appears as a column rather than a row. 如果我将在第二次测试中进行替换,则显示为列而不是行。

SELECT * FROM (
    SELECT 'Prüfung1' AS "Prüfung", (CASE 
    WHEN (SELECT count(*) FROM TableA) = (SELECT count(*) FROM TableB)
    THEN 'OK!' 
    ELSE '!!! Anzahl stimmt nicht überein !!!' END) "Ergebnis" FROM dual) check1,
    (
    SELECT 'Prüfung2' AS "Prüfung", (CASE 
    WHEN (SELECT count(*) FROM TableC) = (SELECT count(*) FROM TableD)
    THEN 'OK!' 
    ELSE '!!! Anzahl stimmt nicht überein !!!' END) "Ergebnis" FROM dual) check2;

My Result should be: 我的结果应该是:

|Tests|Result|
|Check1|Result1|
|Check2|Result2|
|Check3|Result3|
...

How can I do that best? 我怎么能做到最好?

Sorry for my englsih! 对不起我的englsih! ;-) ;-)

Update for @vercelli @vercelli的更新

Here the query with the ORA-00904 Error: 这里查询ORA-00904错误:

with myTables as (select 'tableA' as tableName, count(*) as howMany from MIGRATION_OLDTABLE1 union all
              select 'tableB' as tableName, count(*) as howMany from OLDTABLE1 union all
              select 'tableC' as tableName, count(*) as howMany from MIGRATION_OLDTABLE2 union all
              select 'tableD' as tableName, count(*) as howMany from OLDTABLE2
              --....
              --select 'tableN' as tableName, count(*) from TableC
              ),
myRelations (select 'tableA' as newTable, 'tableB' as oldtable from dual union all
             select 'tableC' as newTable, 'tableD' as oldtable from dual )
select DECODE(a1.howMany, a2.howMany, 'OK', 'KO') as results , r.newTable ||' : ' || to_char(a1.howMany) ||' vs. '|| r.oldTable || ' : ' || to_char(a2.howMany) diff
from myTables a1 join myRelations r on a1.tableName = r.newTable
               join myTables a2 on a2.tableName = r.oldTable;

I'd create a subquery with the count(*) of all the tables with their tableName . 我将使用tableName创建一个子查询,其中包含所有表的count(*) Another subquery with all the relations ( newTable vs. oldTable ) Then join them and compare the count(*) . 另一个具有所有关系的子查询( newTableoldTable )然后加入它们并比较count(*)

with myTables as (select 'tableA' as tableName, count(*) as howMany from tableA union all
                  select 'tableB' as tableName, count(*) as howMany from tableB union all
                  ....
                  select 'tableN' as tableName, count(*) from tableN),
    myRelations as (select 'tableA' as newTable, 'tableB' as oldtable from dual union all
                 select 'tableC' as newTable, 'tableD' as oldtable from dual )
select DECODE(a1.howMany, a2.howMany, 'OK', 'KO') as results , r.newTable ||' : ' || to_char(a1.howMany) ||' vs. '|| r.oldTable || ' : ' || to_char(a2.howMany) diff
  from myTables a1 join myRelations r on a1.tableName = r.newTable
                   join myTables a2 on a2.tableName = r.oldTable
  ;

SAMPLE OUTPUT 样本输出

results diff
  KO    tableA : 5624 vs. tableB: 5476
  OK    tableC : 1576 vs. tableD: 1576

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM