I am trying to use a SQL instruction on R to JOIN on the nearest past date.
I have the table JOIN :
CODE DATECREATION PRIX
1 4 2015-12-31 813
2 4 2015-12-31 824
3 4 2016-07-20 864
4 4 2016-07-22 318.61
5 4 2016-07-22 256.52
and table TGA :
CODE DATE TGA
1 10 2012-06-01 19.29
2 20 2012-06-01 19.29
3 21 2012-06-01 19.29
4 23 2012-06-01 19.29
5 35 2012-06-01 19.29
I want to join that dataframes by CODE and DATE. But the dates are not the same, I want all lines of table JOIN, and take the nearest date of TGA to add the column TGA on the dataframe JOIN
I tried :
TEST = sqldf("SELECT A.*,
(SELECT TOP 1 B.TGA
FROM dataTGA B
WHERE B.DATEDEBUTAPPLICATION < A.DATECREATION
ORDER BY B.DATEDEBUTAPPLICATION DESC) AS TGA
FROM JOIN A")
The error is :
Error in rsqlite_send_query(conn@ptr, statement) : near "1": syntax error
And I didn't add yet the condition on the codes