2014-05-21 41 views
0

我有一個查詢計算兩個日期之間的分鐘差異。我遇到的問題是查詢需要根據已過去的分鐘數返回不同的結果。我想在不重新計算每個病例的分鐘的情況下做到這一點。如果可能的話,我想在一個簡單的查詢中做到這一點。在Case語句中計算的Oracle簡單查詢

Ex。我想刪除(d2-d1)* 1440並保存到一個變量中,以便它可以在查詢中再次使用。

select a, b, c, 
(case 
when (d2-d1)*1440 < 10 then 'Less than 10' 
when (d2-d1)*1440 < 500 then 'Less than 500' 
else 'More than 500' 
end 
) 
from my_table 

回答

0

使用在線查詢,將有助於得到期望的結果

select a, b, c, 
(case 
when ddiff < 10 then 'Less than 10' 
when ddiff < 500 then 'Less than 500' 
else 'More than 500' 
end 
) 
from (select a,b,c, (d2-d1) * 1440 ddiff from my_table) X 
+0

您錯過了子查詢中的ddiff別名。 –

+0

@VictorBruno加入了.. –

0

使用子查詢。

select a, b, c, 
(case 
when min_diff < 10 then 'Less than 10' 
when min_diff < 500 then 'Less than 500' 
else 'More than 500' 
end 
) 
from ( 
    select a,b,c, (d2-d1)*1440 AS min_diff from my_table 
)