dbplyr R中TIMESTAMPDIFF mysql函数在月份中的差异

时间:2018-07-12 16:25:48

标签: r dplyr dbplyr

我正在尝试使用dbplyr软件包计算R中两个日期之间的月份差,我想使用mysql中的“ timestampdiff”本机函数发送sql查询来计算它,但出现错误:

library(tidyverse)
library(lubridate)
library(dbplyr)     

        db_df <- tbl(con, "creditos")

        db_df %>% mutate(diff_month = timestampdiff(month, column_date_1, column_date_2))

但是参数month的翻译不正确,因为它看起来像R中的对象或函数:

  

UseMethod(“ escape”)中的错误:         没有适用于“功能”类对象的“转义”适用方法

如果用这种方式写:

db_df %>% mutate(diff_month = timestampdiff("month", column_date_1, column_date_2))

我也会收到一个错误:

  

您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在monthcolumn_date_1column_date_2)AS diff_month

附近使用正确的语法

我相信这是因为dbplyr正在将带有双引号的“ month”写入mysql,并且应该没有双引号,例如:

TIMESTAMPDIFF(month, column_date_1, column_date_2) AS `diff_month`

还是有更好的方法使用dbplyr计算月份差异?

1 个答案:

答案 0 :(得分:0)

package example.callerid; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.sip.*; import android.os.Build; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; /** * Listens for incoming SIP calls, intercepts and hands them off to CallerIDactivity. */ public class IncomingCallReceiver extends BroadcastReceiver { public Calendar calendar; public SimpleDateFormat dateFormat; public String date; public SipManager mSipManager; public SipSession mSipSesion; public SipProfile peerProfile; static private int notifyNum = 1; /** * Processes the incoming call, answers it, and hands it over to the * CallerIDactivity. * @param context The context under which the receiver is running. * @param intent The intent being received. */ @Override public void onReceive(Context context, Intent intent) { mSipManager = SipManager.newInstance(context); try { mSipSesion = mSipManager.getSessionFor(intent); } catch (SipException e) { e.printStackTrace(); } peerProfile = mSipSesion.getPeerProfile(); calendar = Calendar.getInstance(); dateFormat = new SimpleDateFormat("dd/MM/yyyy"); date = dateFormat.format(calendar.getTime()); Log.d("CallerID", "OnReceive"); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "CHANNEL_ID") .setSmallIcon(R.mipmap.ic_launcher_round) .setContentTitle("CallerID" + " " + date) .setContentText(peerProfile.getDisplayName()) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); if(notifyNum <= 9) { notificationManager.notify(notifyNum++, mBuilder.build()); }else {notificationManager.notify(notifyNum, mBuilder.build()); notifyNum = 1; } int count = 0; while (count <= 10) { Toast toast = Toast.makeText(context, peerProfile.getDisplayName(), Toast.LENGTH_SHORT); toast.show(); count++; }//end while }//end onReceive }//end Class month包中的函数。好像mutate是作为R函数lubridate而不是文本传递给month的。

如果您正在使用本机SQL计算时差,则不需要lubridate程序包。

两种可能的解决方案:

  1. 从您的前言中删除month(),并使用前缀library(lubridate)引用lubridate软件包。例如:lubridate::
  2. 将要在本机SQL中运行的mutate命令的部分大写。这应该有助于SQL转换将它们与具有其他含义的小写变体区分开。例如:lubridate::ymd_hms