Microsoft SQL 查询 return 一行中的重复行
Microsoft SQL Query return repeated rows in one row
我需要你的帮助来查询:
SELECT
'ABBOTT' AS Customername, 'DaleelMAMA' AS AccountName,
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
这个查询 return 这个结果集:
原结果
ABBOTT DaleelMAMA Eman 55555555 NULL 56545412 Arabic
ABBOTT DaleelMAMA Eman 55555555 NULL 22222222 Arabic
ABBOTT DaleelMAMA Eman 55555555 NULL 25456552 Arabic
ABBOTT DaleelMAMA Hala 55552504 22252128 NULL Arabic
但我需要将查询更新为 return 这个结果,但如下所示,如果你看到你会发现 Eman 行重复了三次,这是因为它包含不同的 parent phone 号码,所以我需要在一行中生成此结果,包括母亲号码及其相关 parent phones 号码。
要求的结果
请参阅上面link所需的结果,旨在寻找它,提前感谢您的支持。
整个解决方案外观
如果它总是 4 ParentPhone
每个 Name
然后尝试这样的事情
;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
注意: 如果每个 Name
的 ParentPhone
数量未知,那么您需要使用动态数据透视。也开始使用别名使查询更具可读性
我需要你的帮助来查询:
SELECT
'ABBOTT' AS Customername, 'DaleelMAMA' AS AccountName,
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
这个查询 return 这个结果集:
原结果
ABBOTT DaleelMAMA Eman 55555555 NULL 56545412 Arabic
ABBOTT DaleelMAMA Eman 55555555 NULL 22222222 Arabic
ABBOTT DaleelMAMA Eman 55555555 NULL 25456552 Arabic
ABBOTT DaleelMAMA Hala 55552504 22252128 NULL Arabic
但我需要将查询更新为 return 这个结果,但如下所示,如果你看到你会发现 Eman 行重复了三次,这是因为它包含不同的 parent phone 号码,所以我需要在一行中生成此结果,包括母亲号码及其相关 parent phones 号码。
要求的结果
请参阅上面link所需的结果,旨在寻找它,提前感谢您的支持。
整个解决方案外观
如果它总是 4 ParentPhone
每个 Name
然后尝试这样的事情
;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
注意: 如果每个 Name
的 ParentPhone
数量未知,那么您需要使用动态数据透视。也开始使用别名使查询更具可读性