sql—在目标表中插入源表行中每列的新条目

nwsw7zdq  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(295)

如果题目让人困惑,我很抱歉。我有一个非常大的源表,它的结构有点像这样

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
------------------------------------------------------------------------

在这里,你可以选择一个特定的milage,例如23,和一个特定的weight,例如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

遍历源表并按上述格式插入数据的最佳方法是什么?大约有数百行和列,因此命名每一行和列类似于手动输入数据。
下面是一个sqlfiddle和源表的示例:http://sqlfiddle.com/#!18/48777a/1号

nue99wik

nue99wik1#

你可以用 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

相关问题