简体   繁体   English

一年中的一周使用 bigquery/bigrquery 和 dbplyr? (相当于 lubridate::week)

[英]Week of year using bigquery / bigrquery and dbplyr? (equivalent of lubridate::week)

I'm trying to use bigrquery and dbplyr to get the week of the year that a date corresponds to (ie the same as lubridate::week() , ie我正在尝试使用 bigrquery 和 dbplyr 来获取日期对应的一年中的lubridate::week() (即与lubridate::week() ,即

library(lubridate)
library(dbplyr)
library(bigrquery)

week("2015-08-11")   
# [1] 32

but I am using bigrquery and dbplyr但我正在使用bigrquerydbplyr

What I've tried so far到目前为止我尝试过的

Using lubridate::week() I see使用lubridate::week()我看到

transactions %>% 
  select(item, date) %>% 
  mutate(week = week(date)) %>%
  collect()
Error: Function not found: week at [1:30] [invalidQuery]

So I attempted this home made solution所以我尝试了这个自制的解决方案

transactions %>% 
  select(item, date) %>%
  mutate(week = strftime(date, format = "%V")) %>% 
  collect()

Error: Syntax error: Expected ")" but got keyword AS at [1:54] [invalidQuery]
In addition: Warning message:
  Named arguments ignored for SQL strftime 

as well as another (fairly ugly) home made solution以及另一个(相当丑陋的)自制解决方案

transactions %>% 
  select(item, date) %>% 
  mutate(week = as.numeric((as.Date(date) - as.Date(paste0(substr(date, 1, 4), "-01-01"))), units="days") %/% 7) %>% 
  collect()

Error in as.numeric((as.Date(date) - as.Date(paste0(substr(date, 1,  : 
unused argument (units = "days")

but I cannot seem to find a way to get the week number using bigquery and dbplyr但我似乎无法找到使用 bigquery 和 dbplyr 获取周数的方法

I cannot seem to find a way to get the week number using bigquery我似乎找不到使用 bigquery 获取周数的方法

Looks like you are looking for below BigQuery Standard SQL function看起来您正在寻找以下 BigQuery 标准 SQL 函数

EXTRACT(WEEK FROM date)    

You can use WEEK or WEEK(< WEEKDAY>) or ISOWEEK您可以使用 WEEK 或 WEEK(< WEEKDAY>) 或 ISOWEEK

See more about date part here https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract在此处查看有关日期部分的更多信息https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM