如果唯一有效的方案是滿山遍野有序,沒有間隙,我認爲這是你說的話,那麼你可以計算出填充的「最高」字段以及填充的字段數量,並對它們進行比較。如果我們有差距,那麼這兩個數字將會不同。例如:
select field1, field2, field3, field4, field5 from (
select field1, field2, field3, field4, field5,
case when field1 is not null then 1 else 0 end
+ case when field2 is not null then 1 else 0 end
+ case when field3 is not null then 1 else 0 end
+ case when field4 is not null then 1 else 0 end
+ case when field5 is not null then 1 else 0 end as cnt,
greatest(case when field1 is not null then 1 else 0 end,
case when field2 is not null then 2 else 0 end,
case when field3 is not null then 3 else 0 end,
case when field4 is not null then 4 else 0 end,
case when field5 is not null then 5 else 0 end) as grt
from my_table
)
where cnt != grt;
SQL Fiddle demo。
由於Michael-O指出,你可以使用的nvl2
代替case
(在another SQL Fiddle所示),這是不規範的,但是可以清晰的:
select field1, field2, field3, field4, field5 from (
select field1, field2, field3, field4, field5,
nvl2(field1, 1, 0) + nvl2(field2, 1, 0) + nvl2(field3, 1, 0)
+ nvl2(field4, 1, 0) + nvl2(field5, 1, 0) as cnt,
greatest(nvl2(field1, 1, 0), nvl2(field2, 2, 0), nvl2(field3, 3, 0),
nvl2(field4, 4, 0), nvl2(field5, 5, 0)) as grt
from t42
)
where cnt != grt;
如果你想執行這個你可以添加一個檢查約束做同樣的比較:
alter table my_table add constraint my_check check (
case when field1 is not null then 1 else 0 end
+ case when field2 is not null then 1 else 0 end
+ case when field3 is not null then 1 else 0 end
+ case when field4 is not null then 1 else 0 end
+ case when field5 is not null then 1 else 0 end
= greatest(case when field1 is not null then 1 else 0 end,
case when field2 is not null then 2 else 0 end,
case when field3 is not null then 3 else 0 end,
case when field4 is not null then 4 else 0 end,
case when field5 is not null then 5 else 0 end));
由於您使用11gR2中,你可以與虛擬列也這麼做:
alter table my_table add cnt generated always as
(case when field1 is not null then 1 else 0 end
+ case when field2 is not null then 1 else 0 end
+ case when field3 is not null then 1 else 0 end
+ case when field4 is not null then 1 else 0 end
+ case when field5 is not null then 1 else 0 end);
alter table my_table add grt generated always as
(greatest(case when field1 is not null then 1 else 0 end,
case when field2 is not null then 2 else 0 end,
case when field3 is not null then 3 else 0 end,
case when field4 is not null then 4 else 0 end,
case when field5 is not null then 5 else 0 end));
alter table my_table add constraint my_check check (cnt = grt);
...但除了檢查本身也許更清澈,我不認爲它增加了很多。
使用'nvl2'而不是'case',它使代碼更具可讀性。 –
@ Michael-O - 我通常更喜歡'case',但我已經添加了'nvl2'版本作爲完美的替代選擇。謝謝。 –
+1。限制。 –