除了通过自行加入您的用户可能拥有多少个电话号码之外,我想不出一种做选择语句的好方法。话虽如此,您可以为您的选择语句尝试这个:
;With CTE_Main as (
Select
id
,Fono
,row_number()
Over(Partition by ID order by Fono) as RN
From sucursales
), CTE_Users as (
Select
id as id_num
from sucursales
group by id
)
Select
id_num
,a.Fono as Phone_1
,b.Fono as Phone_2
,c.Fono as Phone_3
,d.Fono as Phone_4
,e.Fono as Phone_5
From CTE_Users as realz
Left Join [CTE_Main] as a on a.id = realz.id_num and a.RN = 1
Left Join [CTE_Main] as b on b.id = realz.id_num and b.RN = 2
Left Join [CTE_Main] as c on c.id = realz.id_num and c.RN = 3
Left Join [CTE_Main] as d on d.id = realz.id_num and d.RN = 4
Left Join [CTE_Main] as e on e.id = realz.id_num and e.RN = 5
我知道它有点冗长,但它会以你想要的方式显示结果。我的例子只使用了 5 行,但它应该很容易解释。
Sql 小提琴:http ://sqlfiddle.com/#!3/496f6/1