我有一个客户端继承了一个Excel电子表格,其中包含邮件列表的联系信息。显然,他的前任试图格式化电子表格以打印针脚邮寄标签。它使用一列,并在不同的行中包含地址信息:
Name
Street Address
City, ST ZIP
Name
Street Address
City, ST ZIP
Name
Street Address
City, ST ZIP
我需要做的是将数据转换为列,使用单独的City,ST和ZIP列(用于运营商路线排序)。我最初的想法是使用Pivot,但我似乎无法让它工作。
此邮件列表中有1,800多个名称,因此无法手动修复它。我也有想法将它们导出为CSV,然后使用一系列查找和替换操作来用逗号替换换行符,但这似乎是丛林联盟。必须有适当的数据解决方案。
想法?
谢谢,
TY
答案 0 :(得分:0)
这是我的解决方案。它基于这样的假设,即您所描述的结构是固定且一致的。
它是在书面T-SQL(SQL Server)中。将它转换为my-sql时不应该有任何问题。
我反对vba解决方案。 SQL非常强大。
如果您很难实现这一点,我还有一些想法。 (我只是不确定my-sql中可用的内容并且必须检查它)。任何方式 - 它应该工作。
--DROP TABLE Example_Table;
CREATE TABLE Example_Table
(
Val VARCHAR(1000),
Row_Num INT IDENTITY(1,1)
);
INSERT INTO Example_Table
SELECT 'John' AS Val UNION ALL
SELECT 'Elm St.' AS Val UNION ALL
SELECT 'Tel Aviv, 151515' AS Val UNION ALL
SELECT 'Doe' AS Val UNION ALL
SELECT 'Manhatten St.' AS Val UNION ALL
SELECT 'Jerusalem, 344343' AS Val UNION ALL
SELECT 'Fox' AS Val UNION ALL
SELECT 'Mulder St.' AS Val UNION ALL
SELECT 'San Francisco, 3243424' AS Val UNION ALL
SELECT 'Jean Lic' AS Val UNION ALL
SELECT 'Picard St.' AS Val UNION ALL
SELECT 'Enterprise City, 3904734' AS Val;
SELECT Name_Details.Val AS Name
, Street_Details.Val AS Street
, SUBSTRING(City_Details.Val, 1, CHARINDEX(',', City_Details.Val, 0) - 1) AS City
, SUBSTRING(City_Details.Val, CHARINDEX(',', City_Details.Val, 0) + 2, LEN(City_Details.Val) - CHARINDEX(',', City_Details.Val, 0)) AS Zip
, City_Details.Val AS City_And_Zip
FROM Example_Table AS Name_Details
INNER JOIN
(SELECT VAL, Row_Num FROM Example_Table AS E WHERE Row_Num % 3 = 2) AS Street_Details
ON (Street_Details.Row_Num = Name_Details.Row_Num + 1)
INNER JOIN
(SELECT VAL, Row_Num FROM Example_Table AS E WHERE Row_Num % 3 = 0) AS City_Details
ON (City_Details.Row_Num = Name_Details.Row_Num + 2)
WHERE Name_Details.Row_Num % 3 = 1 ;
DROP TABLE Example_Table;