2016-02-25 85 views
0

我想這一點,和它的作品,如何篩選出空值相比較,則甲骨文

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) 

兩次。

回答

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' 
+0

很好的解決,謝謝! – user92322

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'; 
0

我不認爲你需要過濾出空長度,因爲它們不會出現,如果你的子集的長度(即wh在不使用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' 
; 

會做的伎倆