我有一个包含地址详情的表格如下。 +表视图
-ClientID|Address Code|EMAIL
4566666|02|eeeee@gmail.com
4566666|03|77eee@gmail.com
1566656|01|HHHHH@gmail.com
1566656|02|JJJJJ@gmail.com
1566656|03|BBBBB@gmail.com
预期结果:
ClientID|Address code 1|EMAIL 1| Address code 2| Email 2|Address code 3| Email 3|
4566666|02|eeeee@gmail.com|03|77eee@gmail.com
1566656|01|HHHHH@gmail.com|02|JJJJJ@gmail.com|03|BBBBB@gmail.com
答案 0 :(得分:1)
with cte as (
select
row_number() over(partition by "ClientID" order by "AddressCode") AS row_num,
"ClientID",
"AddressCode",
"EMAIL"
from Table1
)
select
"ClientID",
max(case when row_num = 1 then "AddressCode" end) as AddressCode1,
max(case when row_num = 1 then "EMAIL" end) as Email1,
max(case when row_num = 2 then "AddressCode" end) as AddressCode2,
max(case when row_num = 2 then "EMAIL" end) as Email2,
max(case when row_num = 3 then "AddressCode" end) as AddressCode3,
max(case when row_num = 3 then "EMAIL" end) as Email3
from cte
group by "ClientID"