2016-01-05 16 views
0

我正在嘗試使用ORACLE查詢來解決此問題。以下是樣本數據在oracle中查詢別名列時發出的問題

|  Col1  |  Col2 | 
------------------------------------ 
| 21-dec-15  | nochange  | 
| 20-dec-15  | change  | 
| 20-dec-15  | nochange  | 
| 18-dec-15  | change  | 
| 18-dec-15  | nochange  | 

此處col2是別名列,而不是表中的列。

這裏的要求是我需要檢查是否有任何變化發生的具體日期,如果有變化,那麼更新nochangechange那個日期。

由於col2是一個別名列,所以我不知道如何檢查它。如果我們將結果存儲在單獨的別名列中,我也可以。

預期結果:

|  Col1  |  Col2 | 
------------------------------------ 
| 21-dec-15  | nochange  | 
| 20-dec-15  | change  | 
| 20-dec-15  | change  | 
| 18-dec-15  | change  | 
| 18-dec-15  | change  | 
+0

什麼是「參考列」? –

+0

這是一個別名列..編輯.. –

+0

仍然沒有得到它 - 什麼是「別名列」?你的意思是它是在select語句中計算的,比如'select case when ... then'change'else'nochange'end'? –

回答

2

這應做到:

with tempt as 
(
    <<Your existing query that returns col1 and col2>> 
) 
select t1.col1, 
     case when exists (select 1 from tempt t2 where t1.col1=t2.col1 and t2.col2='change') 
      then 'change' 
      else t1.col2 
     end 
    from tempt t1; 

如果需要通過COL1添加訂單。