简体   繁体   中英

Loop over one table, subselect another table and update values of first table with SQL/VBA

I have a source table that has a few different prices for each product (depending on the order quantity). Those prices are listed vertically, so each product could have more than one row to display its prices.

Example:

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

The other table I have is the result table in which there is only one row for each product, but there are five columns that each could contain the quantity and price for each row of the source table.

Example:

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

Result:

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

Here is my Python/SQL solution for this (I'm fully aware that this could not work in any way, but this was the only way for me to show you my interpretation of a solution to this problem):

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)

I honestly have no idea how to do this with only SQL or VBA (those are the only languages I'd be able to use -> MS-Access).

This is a pain in MS Access. If you can enumerate the values, you can pivot them.

If we assume that price is unique (or quantity or both), then you can generate such a column:

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;

I should note that another solution would use data frames in Python. If you want to take that route, ask another question and tag it with the appropriate Python tags. This question is clearly a SQL question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM