Difference between dates in SQLDF in R

本小妞迷上赌 提交于 2021-02-08 07:37:03

问题


I am using the R package SQLDF and am having trouble finding the number of days between two date time variables. The variables ledger_entry_created_at and created_at are Unix Epochs and when I try to subtract them after casting to julianday, I return a vector of NA's.

I've taken a look at this previous question and didn't find it useful since my answer to be given in SQL for reasons that are outside the scope of this question.

If anyone could help me figure out a way to do this inside SQLDF I would be grateful.

EDIT:

    SELECT strftime('%Y-%m-%d %H:%M:%S', l.created_at, 'unixepoch') ledger_entry_created_at, 
      l.ledger_entry_id, l.account_id, l.amount, a.user_id, u.created_at
  FROM ledger l
  LEFT JOIN accounts a
  ON l.account_id = a.account_id
  LEFT JOIN users u
  ON a.user_id = u.user_id

回答1:


This answer is trivial, but if you already have two UNIX timestamps, and you want to find out how many days have elapsed between them, you can simply take the difference in seconds (their original unit), and convert to days, e.g.

SELECT
    (l.created_at - u.created_at) / (3600*24) AS diff
    -- any maybe other columns here
FROM ledger l
LEFT JOIN accounts a
    ON l.account_id = a.account_id
LEFT JOIN users u
    ON a.user_id = u.user_id;

I don't know why your current approach is failing, as the timestamps you have in the screen capture should be valid inputs to SQLite's julianday function. But, again, you may not need such a complicated route to get the result you want.



来源:https://stackoverflow.com/questions/52307241/difference-between-dates-in-sqldf-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!