我有一個查詢,需要檢查所有字段的值都在有效代碼列表中。現在我一次又一次地調用同一個子查詢。我想抽象出子查詢,以便它更快,代碼不重複。這是有問題的查詢:如何抽象出子查詢?
select count(*)
into cnt
from pdv_validcodes c
where c.code_type = 'YNNA'
and (upper(:new.spec_1) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_1 is null)
and (upper(:new.spec_2) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_2 is null)
and (upper(:new.spec_3) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_3 is null)
and (upper(:new.spec_4) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_4 is null)
and (upper(:new.spec_5) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_5 is null)
and (upper(:new.spec_6) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_6 is null)
and (upper(:new.spec_7) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_7 is null)
and (upper(:new.spec_8) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_8 is null)
and (upper(:new.spec_9) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_9 is null)
and (upper(:new.spec_10) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.spec_10 is null)
and (upper(:new.add_spec_1) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_1 is null)
and (upper(:new.add_spec_2) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_2 is null)
and (upper(:new.add_spec_3) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_3 is null)
and (upper(:new.add_spec_4) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_4 is null)
and (upper(:new.add_spec_5) in
(select code from pdv_validcodes where code_type = 'YNNA') or
:new.add_spec_5 is null);
作爲一個方面說明,這是一個非常可怕的,懶惰的數據庫設計... – Blindy
這絕對是。另一組列可以達到33(在同一張表中)。儘管它來自外部,但遺憾的是沒有太多可以做到。 –
如果它來自外部源並加載到數據庫中,請記住ETL中的「T」代表轉換 –