规范化一对 CSV 表中的数据
Normalizing data from a pair of CSV tables
我正在尝试规范化一些数据,但似乎无法提出解决方案。我有的是这样的 table:
weight position1 position2 position3
1 10 20 30
2 25 35 45
3 17 05 22
还有一个像这样的:
location position
6 1
7 1
8 2
9 2
10 2
11 3
12 3
如何规范化上述内容,以便在给定位置和权重的情况下找到给定位置的值?
我可以使用 Perl、Python、Excel、MySQL 或块上的几乎任何工具来对数据进行实际的重组;我遇到的问题是想出一个合理的模式。
此处期望的结果类似于
if location == 11 -> position is 3
因此,
if weight == 2 -> the value is 45
唯一要做的就是"unpivot"你的第一个table:
weight position value
1 1 10
1 2 20
1 3 30
2 1 25
2 2 35
2 3 45
3 1 17
3 2 05
3 3 22
前两列应包含唯一的值对。如果您有其他仅取决于重量的信息,则需要另一个 table。位置相同。
正在转换为新模型
如果您已经有了 tables,那么您可以使用以下语句创建第一个 table (t1):
create table t1_new
select weight, 1 as position, position1 as value
from t1
union all
select weight, 2 as position, position2 as value
from t1
union all
select weight, 3 as position, position3 as value
from t1
然后,验证结果后,drop t1,将t1_new重命名为t1。
从新模型查询
要从这些 table 中查询给定位置和体重的值,您应该使用联接:
select value
from t1
inner join t2 on t2.weight = t1.weight
where t2.location = 11
and t1.position = 3
我正在尝试规范化一些数据,但似乎无法提出解决方案。我有的是这样的 table:
weight position1 position2 position3
1 10 20 30
2 25 35 45
3 17 05 22
还有一个像这样的:
location position
6 1
7 1
8 2
9 2
10 2
11 3
12 3
如何规范化上述内容,以便在给定位置和权重的情况下找到给定位置的值?
我可以使用 Perl、Python、Excel、MySQL 或块上的几乎任何工具来对数据进行实际的重组;我遇到的问题是想出一个合理的模式。
此处期望的结果类似于
if location == 11 -> position is 3
因此,
if weight == 2 -> the value is 45
唯一要做的就是"unpivot"你的第一个table:
weight position value
1 1 10
1 2 20
1 3 30
2 1 25
2 2 35
2 3 45
3 1 17
3 2 05
3 3 22
前两列应包含唯一的值对。如果您有其他仅取决于重量的信息,则需要另一个 table。位置相同。
正在转换为新模型
如果您已经有了 tables,那么您可以使用以下语句创建第一个 table (t1):
create table t1_new
select weight, 1 as position, position1 as value
from t1
union all
select weight, 2 as position, position2 as value
from t1
union all
select weight, 3 as position, position3 as value
from t1
然后,验证结果后,drop t1,将t1_new重命名为t1。
从新模型查询
要从这些 table 中查询给定位置和体重的值,您应该使用联接:
select value
from t1
inner join t2 on t2.weight = t1.weight
where t2.location = 11
and t1.position = 3