我有一个Excel表(可以转换为XML或CSV进行操作)这个结构:
| License-plate | Parking | Fuel | Cleaning |
---------------------------------------------
| 1111AAA | 234 | 21 | 1244 |
| 2222AAA | 22 | 12 | 644 |
| 3333BBB | 523 | 123 | 123 |
每辆车/月停车,燃油等每月支出。
车牌是表中的唯一值。
我需要在此转换此表以将其导入MySQL,但我不知道如何做到这一点以及哪个工具对它有好处:
| License-plate | Concept | Amount |
-------------------------------------
| 1111AAA | Parking | 234 |
| 1111AAA | Fuel | 21 |
| 1111AAA | Cleaning | 1244 |
| 2222AAA | Parking | 22 |
| 2222AAA | Fuel | 12 |
| 2222AAA | Cleaning | 644 |
| ....... | ........ | ..... |
在结果表中,牌照不是唯一值,并且它已经重复了它的概念数量。
UPD:刚刚发现它可以被称为非规范化数据(可能不完全正确)。
答案 0 :(得分:0)
我会用MySQL做到这一点,方法如下:
将表格(将其转换为CSV后)导入MySQL。我们称之为 source
CREATE TABLE source (
License_Plate char(7) primary key,
Parking int(8) unsigned,
Fuel int(8) unsigned,
Cleaning int(8) unsigned
);
LOAD DATA INFILE 'path/to/file' INTO TABLE source FIELDS TERMINATED BY ',';
创建另一个具有所需最终结构的表格,让我们称之为目的地
CREATE TABLE destination (
License_Plate char(7),
Concept varchar(10),
Amount int(8) unsigned
);
执行以下查询
INSERT INTO destination
SELECT License_Plate, 'Parking' as Concept, Parking as Amount
FROM source
INSERT INTO destination
SELECT License_Plate, 'Fuel' as Concept, Fuel as Amount
FROM source
INSERT INTO destination
SELECT License_Plate, 'Cleaning' as Concept, Cleaning as Amount
FROM source
需要考虑的事项:
希望这会对你有所帮助。
答案 1 :(得分:0)
@pnuts的评论帮助了我。这是非常简单的解决方案,可以在Excel中完成。谢谢!
解决方案是:Convert matrix to 3-column table ('reverse pivot', 'unpivot', 'flatten', 'normalize')