迁移后用于分析数据的SQL

时间:2016-08-23 09:19:45

标签: sql oracle

我迁移了大量数据,现在想检查它们是否正确。

下面是一个简单的例子: 我已经迁移了一张已经填满另一张桌子的桌子。 现在我想检查两个表的数量并进行比较。 另外我创建了以下查询:

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

但是,我会进行几次这样的测试,并显示一个结果。如果我将在第二次测试中进行替换,则显示为列而不是行。

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;

我的结果应该是:

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

我怎样才能做到最好?

对不起我的englsih! ; - )

@vercelli的更新

此处查询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;

1 个答案:

答案 0 :(得分:0)

我创建了一个子查询,其中包含count(*)所有表格的tableName。 包含所有关系的另一个子查询(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