2016-08-23 57 views
1

我有大量的數據遷移,現在想檢查它們的正確性。SQL在遷移後分析數據

下面是一個簡單的例子: 我已經遷移了一個已經填充在另一個表中。 現在我想檢查兩個表的數量並進行比較。 此外,我已經創建了下面的查詢:

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; 
+0

我不知道你真正想要的結果。只有那些排數不同的表格?或者具有不同值的行?請[編輯]你的問題,並根據這些數據添加一些樣本數據和預期的輸出。 _Formatted_文本,請[無屏幕截圖](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-問題/ 285557#285557) –

+0

使用'union all'。 –

回答

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 
    ; 

樣本輸出

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

我得到了一個ORA-00904錯誤! –

+0

標識符無效?請發佈您的查詢。 – vercelli

+0

也許我誤解了tableN。哪裏放我表C和D? –