如何过滤掉null值然后在oracle中进行比较

时间:2016-02-25 22:47:48

标签: oracle

我尝试了这个,它有效,

SELECT name    
FROM    
  (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
  )    
WHERE LENGTH >= ALL    
  (SELECT LENGTH FROM    
    (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
    )    
  ) 

但我的最终代码是这样的:

SELECT a.name,    
  a.length    
FROM    
  (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
  ) a,    
  geo_river b,    
  encompasses c    
WHERE a.length >= ALL    
  (SELECT a2.LENGTH    
  FROM    
    (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL    
    ) a2    
  )    
AND a.name      = b.river    
AND b.country   = c.country   
AND c.continent = 'America'

这真的很复杂。 是否有一种简单的方法让

(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL)

是河,所以我不需要使用这个

(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL)

两次。

3 个答案:

答案 0 :(得分:0)

如果您想简化代码编写,可以使用WITH

with viewA as (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL )
SELECT a.name,    
  a.length    
FROM    
  viewA a,    
  geo_river b,    
  encompasses c    
WHERE a.length >= ALL    
  (SELECT a2.LENGTH    
  FROM    
    viewA a2    
  )    
AND a.name      = b.river    
AND b.country   = c.country   
AND c.continent = 'America'

答案 1 :(得分:0)

使用单表扫描:

SELECT name,
       length
FROM   (
  SELECT name,
         length,
         RANK() OVER ( ORDER BY length DESC ) AS rnk
  FROM   river
)
WHERE rnk = 1;

那么你的代码就是:

SELECT a.name,    
       a.length    
FROM   (
         SELECT name,
                length
         FROM   (
           SELECT name,
                  length,
                  RANK() OVER ( ORDER BY length DESC ) AS rnk
           FROM   river
         )
         WHERE rnk = 1
       ) a
       INNER JOIN
       geo_river b
       ON ( a.name    = b.river )
       INNER JOIN
       encompasses c
       ON ( b.country = c.country )   
WHERE  c.continent = 'America';

答案 2 :(得分:0)

我认为您不需要过滤掉null长度,因为如果您按长度进行子集(例如,在不使用NVL函数的情况下比较NULL值时,比较将始终评估为false并且不显示行)它们将不会显示)。所以简单的事情如下:

Select a.name, a.length 
from river a, geo_river b,  encompasses c   
WHERE a.length > 0 
AND a.name      = b.river  
AND b.country   = c.country 
AND c.continent = 'America' 
;

会做的伎俩