다음이
;with cte as
(
SELECT
'ABBOTT' AS Customername,
'DaleelMAMA' AS AccountName,
ROW_NUMBER() over(partition by dbo.AccountBase.Name order by dbo.AccountBase.Telephone1) as rn,
dbo.AccountBase.Name AS FullName,
dbo.AccountBase.Telephone1 AS MobilePhone,
dbo.AccountBase.new_HomePhone1 AS HomePhone,
dbo.new_otherparentinformationBase.new_MobilePhone AS ParentPhone,
dbo.AccountBase.new_preferredlanguagetext AS Language
FROM
dbo.AccountBase
LEFT OUTER JOIN
dbo.new_otherparentinformationBase ON dbo.AccountBase.AccountId = dbo.new_otherparentinformationBase.new_MotherName_lookup
)
SELECT Customername,
AccountName,
FullName,
MobilePhone,
Max(CASE WHEN rn = 1 THEN ParentPhone END) AS ParentPhone1,
Max(CASE WHEN rn = 2 THEN ParentPhone END) AS ParentPhone2,
Max(CASE WHEN rn = 3 THEN ParentPhone END) AS ParentPhone3,
Max(CASE WHEN rn = 4 THEN ParentPhone END) AS ParentPhone4,
Language
FROM cte
GROUP BY Customername,
AccountName,
FullName,
MobilePhone,
Language
주 같은 시도 그것은 항상 4 ParentPhone
Name
당 경우 : 당신이 필요로 Name
당 ParentPhone
의 수를 알 수없는 경우을 동적 피벗을 사용합니다. 또한 별칭 이름을 사용하여 쿼리를 더 읽기 쉽게 만들 수 있습니다.
사용자가 4 개 이상의 전화 번호를 가질 수 있는지 여부? –