答案 0 :(得分:1)
您可以使用row_number()
并进行条件聚合:
select code,
max(case when seq = 1 then compcode end) as one,
max(case when seq = 2 then compcode end) as two,
. . .
max(case when seq = 7 then compcode end) as seven
from (select t.*,
row_number() over (partition by code order by compcode) as seq
from table t
) t
group by code;
如果PIVOT
有太多的code
,则可以通过动态compcode
SQL实现相同的目的。