2015-04-02 19 views

回答

0

與SQL的問題是,查詢有一個固定的一組列。所以,如果你只有兩個成果,那麼這是可能的 - 你知道結果集有三列。但是,要推廣到更多列,您需要使用動態SQL。

select fruit, 
     max(case when seqnum = 1 then pax end) as col1, 
     max(case when seqnum = 2 then pax end) as col2 
from (select pax, fruit, 
      row_number() over (partition by fruit order by (select null)) as seqnum 
     from table t 
    ) t 
group by fruit; 
相關問題