【问题标题】:Loop over one table, subselect another table and update values of first table with SQL/VBA循环一个表,选择另一个表并使用 SQL/VBA 更新第一个表的值
【发布时间】:2019-01-26 09:14:36
【问题描述】:

我有一个源表,其中每个产品都有几个不同的价格(取决于订单数量)。这些价格是垂直列出的,因此每个产品可以有不止一行来显示其价格。

例子:

ID  | Quantity | Price
--------------------------
001 |    5     | 100
001 |    15    | 90
001 |    50    | 80
002 |    10    | 20
002 |    20    | 15
002 |    30    | 10
002 |    40    | 5

我的另一个表是结果表,其中每个产品只有一行,但有五列,每列可以包含源表每一行的数量和价格。

例子:

ID  | Quantity_1 | Price_1  | Quantity_2 | Price_2  | Quantity_3 | Price_3  | Quantity_4 | Price_4 | Quantity_5 | Price_5
---------------------------------------------------------------------------------------------------------------------------
001 |            |          |            |          |            |          |            |         |            |
002 |            |          |            |          |            |          |            |         |            |

结果:

ID  | Quantity_1 | Price_1  | Quantity_2 | Price_2  | Quantity_3 | Price_3  | Quantity_4 | Price_4 | Quantity_5 | Price_5
---------------------------------------------------------------------------------------------------------------------------
001 |     5      |    100   |     15     |    90    |     50     |    80    |            |         |            |
002 |     10     |    20    |     20     |    15    |     30     |    10    |     40     |    5    |            |

这是我的 Python/SQL 解决方案(我完全知道这无法以任何方式工作,但这是我向您展示我对该问题的解决方案的解释的唯一方法):

For Each result_ID In result_table.ID:
    Subselect = (SELECT * FROM source_table WHERE source_table.ID = result_ID ORDER BY source_table.Quantity) # the Subselect should only contain rows where the IDs are the same

    For n in Range(0, len(Subselect)): # n (index) should start from 0 to last row - 1
        price_column_name = 'Price_' & (n + 1)
        quantity_column_name = 'Quantity_' & (n + 1)

        (UPDATE result_table 
        SET result_table.price_column_name = Subselect[n].Price, # this should be the price of the n-th row in Subselect
            result_table.quantity_column_name = Subselect[n].Quantity # this should be the quantity of the n-th row in Subselect
        WHERE result_table.ID = Subselect[n].ID)

老实说,我不知道如何仅使用 SQL 或 VBA(这些是我能够使用的唯一语言 -> MS-Access)来做到这一点。

【问题讨论】:

    标签: sql loops ms-access select subquery


    【解决方案1】:

    这是 MS Access 的一个难题。如果您可以枚举这些值,则可以旋转它们。

    如果我们假设价格是唯一的(或数量或两者兼有),那么您可以生成这样一个列:

    select id,
           max(iif(seqnum = 1, quantity, null)) as quantity_1,
           max(iif(seqnum = 1, price, null)) as price_1,
           . . . 
    from (select st.*,
                 (select count(*)
                  from source_table st2
                  where st2.id = st.id and st2.price >= st.price
                 ) as seqnum
          from source_table st
         ) st
    group by id;
    

    我应该注意到另一种解决方案将在 Python 中使用数据帧。如果你想走这条路,问 another 问题并用适当的 Python 标记对其进行标记。这道题分明是SQL题。

    【讨论】:

      猜你喜欢
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多