到目前为止一切顺利,只是示例数据。现在做一个 UNPIVOT,然后 PARTITION BY val,ORDER BY C 值,然后 PIVOT 回来:
with t2 as (select * from t1 unpivot (c for cval in (c1 as 'A', c2 as 'B', c3 as 'C')) order by val, c),
t3 as (select val, row_number() over (partition by val order by c) as rank, c from t2)
select * from t3 pivot (min(c) as c for rank in (1,2,3)) order by val;
VAL 1_C 2_C 3_C
----- ---------- ---------- ----------
val1 2 5 10
val2 1 3 7
val3 4 4 8
val4 1 2 9
val5 3 3 5
val6 2 8 10
好吧,请看下文。这是我绝对不会在真实环境中写下的东西(我会质疑原来的问题)。
到目前为止一切顺利,只是示例数据。现在做一个 UNPIVOT,然后 PARTITION BY val,ORDER BY C 值,然后 PIVOT 回来:
限制: