2017-04-20 106 views
0

所以我有一個名爲表1,有兩列,產品和指標表。PLSQL查詢一個變量和劈裂成兩列

Table1 

Product Indicator 
Product 1  Y 
Product 1  Y 
Product 1  Y 
Product 1  N 
Product 1  N 
Product 2  Y 
Product 2  Y 
Product 2  Y 
Product 2  Y 
Product 2  Y 

,我希望能夠運行一個查詢,以顯示提前這樣

  Indicator = Y Indicator = N 
Product 1  Y    Y 
Product 2  Y    N 

感謝的結果! :)

+0

爲什麼你認爲你需要一個存儲過程? –

+0

@a_horse_with_no_name對不起,我是相當新的編碼,你是什麼存儲過程是什麼意思?我想我需要在某處使用「as」Indicator = Y'「和」as'Indicator = N'「?你提到的商店程序是?這是一個商店程序嗎?對不起,如果我錯了。 – Oxy111

+0

那麼你用PL/SQL這是用來編寫Oracle存儲過程標記你的問題。 –

回答

4

無需PL/SQL,這可以用普通的SQL完成的,使用條件聚集。

select product, 
     case 
     when count(case when indicator = 'Y' then 1 end) > 0 then 'Y' 
     else 'N' 
     end as "Indicator = Y", 
     case 
     when count(case when indicator = 'N' then 1 end) > 0 then 'Y' 
     else 'N' 
     end as "Indicator = N" 
from table1 
group by product 
order by product;