避免使用null函数

时间:2017-10-06 07:58:23

标签: sql oracle oracle12c

我有这个查询

select least(                                                              
    (select min(full_date) from lopes.vehicle_data),
    (select min(bithday) from lopes.ad_trf_day),
    (select min(bithday) from lopes.ad_day_rec),
    (select min(bithday) from lopes.ad_day_rec),
    (select min(bithday) from lopes.ad_day),
    (select min(bithday) from lopes.ad_poi_day))
from dual

但它返回NULL。有没有办法避免空值?

1 个答案:

答案 0 :(得分:1)

一种选择是避免使用LEAST

SELECT MIN( dt )
FROM   (
  SELECT full_date FROM lopes.vehicle_data UNION ALL
  SELECT bithday FROM lopes.ad_trf_day UNION ALL
  SELECT bithday FROM lopes.ad_day_rec UNION ALL
  SELECT bithday FROM lopes.ad_day_rec UNION ALL
  SELECT bithday FROM lopes.ad_day UNION ALL
  SELECT bithday FROM lopes.ad_poi_day
)

这仍将返回NULL,但前提是所有表都为空或仅包含NULL值。

另一种方法是使用COALESCENVLCASE

select least(                                                              
         (select COALESCE( min(full_date), DATE '9999-12-31' ) from lopes.vehicle_data),
         (select COALESCE( min(bithday), DATE '9999-12-31' ) from lopes.ad_trf_day),
         (select COALESCE( min(bithday), DATE '9999-12-31' ) from lopes.ad_day_rec),
         (select COALESCE( min(bithday), DATE '9999-12-31' ) from lopes.ad_day_rec),
         (select COALESCE( min(bithday), DATE '9999-12-31' ) from lopes.ad_day),
         (select COALESCE( min(bithday), DATE '9999-12-31' ) from lopes.ad_poi_day)
       )
from dual

这将永远不会返回NULL,但如果所有表都为空或仅包含DATE '9999-12-31'值,则会返回“magic”值NULL