我有这个查询
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。有没有办法避免空值?
答案 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
值。
另一种方法是使用COALESCE
,NVL
或CASE
:
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
。