试图将SQL Server中的行转换为多列,如下所示:
当前结果:
PortCode CarCode
------------------------
AAB A5
ADR BH
SAN QQ预期结果:
PortCode CarCode PortCode CarCode PortCode CarCode
-----------------------------------------------------------
AAB A5 ADR BH SAN QQ曾尝试过与PIVOT,但没有帮助。
谁能给我解释一下,怎么实现呢?
发布于 2018-09-26 10:36:19
如果我没听错,
select max(case when seqnum = 1 then portcode end) as portcode_1,
max(case when seqnum = 1 then carcode end) as carcode_1,
max(case when seqnum = 2 then portcode end) as portcode_2,
max(case when seqnum = 2 then carcode end) as carcode_2,
max(case when seqnum = 3 then portcode end) as portcode_3,
max(case when seqnum = 3 then carcode end) as carcode_3
from (select t.*, row_number() over (order by (select null)) as seqnum
from t
) t;备注:
(select null)。https://stackoverflow.com/questions/52515697
复制相似问题