In Microsoft SQL Server you can define system times for tables and query them like this:
select top 10 * from table FOR SYSTEM_TIME AS OF '2024-01-01'
I would like to use R and dplyr to query the same table. Assuming I have a connection con, how would I do this?
I have tried:
tbl(con, "table FOR SYSTEM_TIME AS OF '2024-01-01'")
and
tbl(con, "table") |>
filter(sql("FOR SYSTEM_TIME AS OF '2024-01-01'"))
Anyone know if it is possible?
The documentation on temporal tables states:
It is possible that R may still observe the temporal columns even if they are hidden. I'd recommend something like the following to check.
If the temporal columns are visible to R, then you should be able to include them in standard
filterclauses.Otherwise, I would recommend making use of the fact that a dbplyr object is essentially two pieces: a database connection and a query. Hence, the following is plausible:
This is similar to code in your question, the key difference being that this needs to be a complete query.
If this works, my inclination would be to make a custom function, something like: