2015-04-02 54 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;