幫財務人員處理數據,一個是ERP系統中的賬面數據,一個是稅務局給的官方數據,一張EXCEL表,想找出兩邊不匹配的數據。
據說EXCEL2007版本已經提供了這種比對的功能,但無奈數據量太大,操作起來巨慢如牛,而WPS2009似乎還沒這個功能,于是導入數據庫中新建一個表存儲這些數據來比對。
開始寫了個SQL來查詢稅務有而ERP系統中沒有的數據:
select * from tab_excel where taxcode not in
(select erpcode from tab_excel)
正常,然后反過來查ERP中存在而稅務系統中不存在的數據:
select * from tab_excel where erpcode not in
(select taxcode from tab_excel)
返回0條數據,很奇怪,然后馬上想到了是null的問題,taxcode必然存在值為null的記錄,oracle中和null比較的返回值是unkown,所以才無法匹配。
于是修改SQL語句如下:
select * from tab_excel tout where not exists
(select 1 from tab_excel where taxcode=tout.erpcode)
結果正常。
not in (...) 括號中的返回值不能存在null值,是Oracle SQL開發的一條鐵律,如果不能確定返回結果一定無null值,還是改寫為not esists吧。而且not in效率低下,一般不能用到索引,生產環境的程序最好不要使用。
posted on 2009-10-20 16:05
Ke 閱讀(264)
評論(0) 編輯 收藏 所屬分類:
oracle