I have a list that contains customers and their ID. It looks like the one below:
customers_id <- list(x = John(1,2,3), Rick = c(4), Sam = c(5,6))
and a database that looks like the one below and calls 'db'
date id value
2017-05-12 1 51
2017-05-13 2 3
2017-05-14 3 217
2017-05-15 1 12
2017-05-16 2 98
2017-05-17 3 123
2017-05-18 1 78
2017-05-19 2 36
2017-05-20 4 178
2017-05-18 5 728
2017-05-19 6 336
2017-05-20 4 718
2017-05-18 5 758
2017-05-19 6 366
2017-05-20 4 787
I tried to make a for loop but couldn't figuer out the right solution. I think that in the loop should be a query that will take a correct ID and sum values for ID
corect_values <- paste(" SELECT date, id, SUM(value) FROM db WHERE id = '", id, "' ")
So I have two issues: How to put a query into for loop and how to write a sql query that will take into account all id for customer.
Result should like the one below:
John 618
Rick 1683
Sam 2188
Do you have any idea how it can be solved ? Thanks for any help !