INSERT INTO 目标 table 源 table 行中每一列的新条目

INSERT INTO target table new entry for each column within a row of source table

如果标题令人困惑,我深表歉意。我有一个非常大的来源table,其结构有点像这样

From Mile  | To Mile |
           |         |From Weight| 1000       | 1100 | 1200 | 1300... | 22500 | 23000
           |         |To Weight  | 1099       | 1199 | 1299 | 1499... | 22999 | 23499
==========================================================================
     1     |   20    |           | 1500       | 1505 | 1489 | 1854... | 3400  | 2990
------------------------------------------------------------------------
     21    |   40    |           | 1400       | 1705 | 491  | 5581... | 1600  | 2989
------------------------------------------------------------------------
     41    |   60    |           | 1800       | 1635 | 1982 | 1888... | 3200  | 3480
------------------------------------------------------------------------

可以选择特定里程数的地方,例如23,和一定的重量,例如1250,并得到 "rate" - 在本例中为 491.

From Mile  | To Mile |
           |         |From Weight|            |      | 1200 |         |       |
           |         |To Weight  |            |      | 1299 |         |       | 
==========================================================================
           |         |           |            |      |      |     ... |       | 
------------------------------------------------------------------------
     21    |   40    |           |            |      | 491  |     ... |       | 
------------------------------------------------------------------------
           |         |           |            |      |      |     ... |       | 
------------------------------------------------------------------------

我想插入每个费率及其相应的重量和英里范围。

Id   | Mile_To | Mile_From | Weight_To | Weight_From | Rate
===========================================================
0    | 1       | 20        | 1000      | 1099        | 1500
-----------------------------------------------------------
1    | 1       | 20        | 1100      | 1199        | 1505
-----------------------------------------------------------
2    | 1       | 20        | 1200      | 1299        | 1389
-----------------------------------------------------------
3    | 1       | 20        | 1300      | 1499        | 1854
-----------------------------------------------------------
...
-----------------------------------------------------------
40   | 1       | 20        | 22500     | 22999       | 3400
-----------------------------------------------------------
41   | 1       | 20        | 23000     | 23499       | 2990
-----------------------------------------------------------
42   | 21      | 40        | 1000      | 1199        | 1400

单步执行源代码 table 并按上述格式插入数据的最佳方法是什么?大约有数百行和列,因此命名每一个都类似于手动输入数据。

这是一个带有源示例的 SQLFiddle table:http://sqlfiddle.com/#!18/48777a/1

您可以使用 cross apply 取消透视此数据集:

select 
    s.from_miles,
    s.to_miles,
    x.*
from source s
cross apply (values
    (1000, 1099, s.[1000_to_1099]),
    (1100, 1199, s.[1100_to_1199]),
    (1200, 1299, s.[1200_to_1299]),
    (1300, 1399, s.[1300_to_1399]),
    (1400, 1499, s.[1400_to_1499])
) as x(weight_from, weigh_to, rate)

您可以轻松地将其转换为 insert 语句:

insert into newtable (from_moles, to_moles, weight_from, weight_to, rate)
select ... -- above query