I have two tables-Price_list
and order_list
. The price_list
table gives me all the prices that were active with date from all stores by product_id
. While order_list
gives me the list of orders placed i.e. who placed the order and from which store.
Price_list - date, product_id, store_id, selling_price
order_list - date, product_id, store_id, selling_price, order_id, email, product_order_id (unique key - concatenation of product_id and order_id as there could more than one product in an order)
I want to combine the above two tables in such a way that for each product_order_id
i get a list of all prices that were available for the product. Basically i want to see what were the prices available and what did the customer choose. The table below illustrates my query.
|product_order_id Date product_id store_id selling_price Placed|
|134323_3545 2016/03/11 134323 6433 2560.00 Yes |
|134323_3545 2016/03/11 134323 6343 2534.00 No |
|134323_3545 2016/03/11 134323 1243 2313.00 No |
|134323_3545 2016/03/11 134323 2424 2354.00 No |
|145565_9965 2016/03/11 145565 9887 5432.00 No |
|145565_9965 2016/03/11 145565 7645 5321.00 Yes |
I am not able to get around to solving this in R. Although i prefer R for this, i am open if there is a solution in mysql or python. The steps to get this done is (a) select product_order_id (B) on that date for each product_id in the product_order_id search for all entries in price_list (C) append this to a table and add a column specifying product_order_id this list applies to (d) repeat the steps for the next product_order_id. Once the dataframe is prepared i can left join order_list table on column(product_order_id) to get the final dataframe. I have not yet been able to grasp how to do it in R.
After reading about loops and some help i was able to create a loop for searching all price entries for each product_id on a day (product_date is a concatenation of date and product_id):
datalist <- list()
for(i in (orderlisit_test$product_date){
dat <- filter(pricelist, pricelist$product_date==i)
datalist[[i]] <- dat
}
big_data = do.call("rbind", datalist)
However, i also want to add another column specifying the order_id or product_order_id for each iteration. So if anyone could help me in how should i loop as well as add another column at the same time that will help me a lot.