SQL Query结合了类似的结果

时间:2015-01-26 04:07:05

标签: sql-server

我在服务器上有一个数据库,我需要提取某些数据以导入到同一产品的较新版本中。问题是旧版本太旧了,制造商没有任何迁移路径到新产品。唯一的方法是手动重新键或手动将数据从SQL数据库导出到新的导入格式。

数据库位于MS SQL Server上,我正在测试环境中处理副本。

我需要的数据基本上是人员联系信息PersonId,FirstName,LastName,Address和联系电话号码(还有其他几个字段)。

我已经编写了一个SQL脚本来生成我需要的数据但是因为一个人可以拥有多个联系号码,它会为同一个人提供多个结果。

例如,假设我们在数据库Bill NoName和Sally Example中有两个人。

比尔只有一个联系号码,他的分机号码,所以他的结果显示为一行。 Sally有三个联系号码,所以我的查询创建了三行。

PersonID, FirstName, LastName, Address, Extension, HomePhone, Mobile,
1049, Bill, NoName, 123 sample st, Any City, Any State, 3343, null, null
1050, Sally, Example, 124 sample st, city, state, 3344, null, null
1050, Sally, Example, 124 sample st, city, state, null, 5555551212, null
1050, Sally, Example, 124 sample st, city, state, null, null, 5555554545

我想将它降低到每个PersonId一行,但是没有成功。

1050, Sally, Example, 124 sample st, city, state, 3344, 5555551212, 5555554545

以下是我创建报告的代码

如何合并或组合多个条目的人?

USE OW5000;
SELECT
Person.PersonId,
LastName,
FirstName, 
MiddleName, 
Note, 
MessageCenterInstructions,
StationNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 8 THEN Extension
ELSE NULL
END,
StationNumber2 = CASE PersonContactMethod.ContactMethodTypeId
WHEN 14 THEN Extension
ELSE NULL
END,
StationNumber3 = CASE PersonContactMethod.ContactMethodTypeId
WHEN 43 THEN Extension
ELSE NULL
END,
WirelessNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 53 THEN Extension
ELSE NULL
END,
MobileNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 45 THEN E164Number
ELSE NULL
END,
HomeNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 2 THEN E164Number
ELSE NULL
END, 
ExternalFax = CASE PersonContactMethod.ContactMethodTypeId
WHEN 54 THEN E164Number
ELSE NULL
END, 
ExternalPrimary = CASE PersonContactMethod.ContactMethodTypeId
WHEN 50 THEN E164Number
ELSE NULL
END,
ExternalSecondary = CASE PersonContactMethod.ContactMethodTypeId
WHEN 51 THEN E164Number
ELSE NULL
END,
AlphaPager = CASE PersonContactMethod.ContactMethodTypeId
WHEN 46 THEN E164Number
ELSE NULL
END,
PhonePager = CASE PersonContactMethod.ContactMethodTypeId
WHEN 7 THEN E164Number
ELSE NULL
END,
Fax = CASE PersonContactMethod.ContactMethodTypeId
WHEN 44 THEN E164Number
ELSE NULL
END,
Line1,
Line2,
City,
StateId,
Zip
From Person
LEFT JOIN PersonAddress
ON Person.PersonId=PersonAddress.PersonId
LEFT JOIN [Address]
ON PersonAddress.AddressId=[Address].AddressId
LEFT JOIN PersonContactMethod
ON Person.PersonId=PersonContactMethod.PersonId
LEFT JOIN ContactMethod
ON PersonContactMethod.ContactMethodId=ContactMethod.ContactMethodId
LEFT JOIN TelephoneNumber
ON ContactMethod.TelephoneNumberId=TelephoneNumber.TelephoneNumberId
ORDER BY LastName, FirstName ASC;

2 个答案:

答案 0 :(得分:0)

为什么不能使用Group by

select PersonID, FirstName, LastName, Address, max(Extension) as Extension, 
       max(HomePhone) as HomePhone, max(Mobile) as Mobile
From yourtable
group by PersonID, FirstName, LastName, Address

答案 1 :(得分:0)

对于那些想要稍后参考修复的人

我将数据转储到临时表,然后执行另一个select和group by。

这是代码。

如果再次发布完整代码,我很抱歉是不正确的协议。

USE OW5000;
/**
This query extracts all the user data from an older version of OW5000. The flaw is that it puts each dam 
telephone number on a seperate line. I tried eliminating it but so far no luck. Still its better than I had earlier. 
**/
SELECT
Person.PersonId,
LastName,
FirstName, 
MiddleName, 
Note, 
MessageCenterInstructions,
Line1,
Line2,
City,
StateId,
Zip,
StationNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 8 THEN Extension
ELSE NULL
END,
StationNumber2 = CASE PersonContactMethod.ContactMethodTypeId
WHEN 14 THEN Extension
ELSE NULL
END,
StationNumber3 = CASE PersonContactMethod.ContactMethodTypeId
WHEN 43 THEN Extension
ELSE NULL
END,
WirelessNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 53 THEN Extension
ELSE NULL
END,
MobileNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 45 THEN E164Number
ELSE NULL
END,
HomeNumber = CASE PersonContactMethod.ContactMethodTypeId
WHEN 2 THEN E164Number
ELSE NULL
END, 
ExternalFax = CASE PersonContactMethod.ContactMethodTypeId
WHEN 54 THEN E164Number
ELSE NULL
END, 
ExternalPrimary = CASE PersonContactMethod.ContactMethodTypeId
WHEN 50 THEN E164Number
ELSE NULL
END,
ExternalSecondary = CASE PersonContactMethod.ContactMethodTypeId
WHEN 51 THEN E164Number
ELSE NULL
END,
AlphaPager = CASE PersonContactMethod.ContactMethodTypeId
WHEN 46 THEN E164Number
ELSE NULL
END,
PhonePager = CASE PersonContactMethod.ContactMethodTypeId
WHEN 7 THEN E164Number
ELSE NULL
END,
Fax = CASE PersonContactMethod.ContactMethodTypeId
WHEN 44 THEN E164Number
ELSE NULL
END
INTO #MyTempTable1
From Person
LEFT JOIN PersonAddress
ON Person.PersonId=PersonAddress.PersonId
LEFT JOIN [Address]
ON PersonAddress.AddressId=[Address].AddressId
LEFT JOIN PersonContactMethod
ON Person.PersonId=PersonContactMethod.PersonId
LEFT JOIN ContactMethod
ON PersonContactMethod.ContactMethodId=ContactMethod.ContactMethodId
LEFT JOIN TelephoneNumber
ON ContactMethod.TelephoneNumberId=TelephoneNumber.TelephoneNumberId
SELECT
#MyTempTable1.PersonId,
#MyTempTable1.LastName,
#MyTempTable1.FirstName, 
#MyTempTable1.MiddleName, 
#MyTempTable1.Note, 
#MyTempTable1.MessageCenterInstructions,
MAX (#MyTempTable1.StationNumber) AS StationNumber,
MAX (#MyTempTable1.StationNumber2) AS StationNumber2,
MAX (#MyTempTable1.StationNumber3) AS StationNumber3,
MAX (#MyTempTable1.WirelessNumber) AS WirelessNumber,
MAX (#MyTempTable1.MobileNumber) AS MobileNumber,
MAX (#MyTempTable1.HomeNumber) AS HomeNumber,
MAX (#MyTempTable1.ExternalFax) AS ExternalFax,
MAX (#MyTempTable1.ExternalPrimary) AS ExternalPrimary,
MAX (#MyTempTable1.ExternalSecondary) AS ExternalSecondary,
MAX (#MyTempTable1.AlphaPager) AS AlphaPager,
MAX (#MyTempTable1.PhonePager) AS PhonePager,
MAX (#MyTempTable1.Fax) AS Fax,
#MyTempTable1.Line1,
#MyTempTable1.Line2,
#MyTempTable1.City,
#MyTempTable1.StateId,
#MyTempTable1.Zip
FROM #MyTempTable1
GROUP BY #MyTempTable1.PersonId, #MyTempTable1.LastName, #MyTempTable1.FirstName, #MyTempTable1.MiddleName, #MyTempTable1.Note, #MyTempTable1.MessageCenterInstructions,#MyTempTable1.Line1,
#MyTempTable1.Line2, #MyTempTable1.City, #MyTempTable1.StateId, #MyTempTable1.Zip
ORDER BY LastName, FirstName ASC
DROP TABLE #MyTempTable1;