SQL 查询以获取时间戳列中没有小时数的数据
SQL query to get data without hours in timestamp column
我已经编写了代码和 sql 查询以从数据库中获取数据:
sql_tables <- glue("
SELECT *
FROM mytable
LIMIT 4
")
table <- dbGetQuery(con, stri_encode(sql_tables, to = "UTF-8")) %>%
as.data.frame()
我得到这个数据框:
ID value timestamp
1 message sent 2019-05-29 06:45:34
2 sold out 2019-05-29 07:55:29
3 processed 2019-05-30 17:42:11
4 processed 2019-05-30 19:44:15
我想编写另一个查询以仅获取 2019-05-29 的数据:
sql_tables <- glue("
SELECT *
FROM mytable
WHERE timestamp = '2019-05-29'
LIMIT 4
")
table <- dbGetQuery(con, stri_encode(sql_tables, to = "UTF-8")) %>%
as.data.frame()
但它给我带来了一个错误:
Error in select(conn@ptr, statement) :
DB::Exception: Key expression contains comparison between inconvertible types: DateTime and String inside timestamp = '2019-05-29'
我该怎么做?我怎样才能摆脱 sql 查询中时间戳列中的小时数?期望的结果是:
ID value timestamp
1 message sent 2019-05-29
2 sold out 2019-05-29
来自manuals data types for date:
A date. Stored in two bytes as the number of days since 1970-01-01
(unsigned). Allows storing values from just after the beginning of the
Unix Epoch to the upper threshold defined by a constant at the
compilation stage (currently, this is until the year 2106, but the
final fully-supported year is 2105).
The date value is stored without the time zone.
所以我们需要将"2019-05-29"
日期转换为数字:
as.numeric(as.Date("2019-05-29"))
# [1] 18045
#Origin is same as clickhouse, test:
as.Date(18045, origin = "1970-01-01")
# [1] "2019-05-29"
现在我们可以将其用作数字(未测试):
SELECT *
FROM mytable
WHERE timestamp = 18045
LIMIT 4
截止日期(时间戳)
SELECT *
从我的表
WHERE toDate(时间戳)='2019-05-29'
限制 4
SELECT *
从我的表
WHERE timestamp => toDateTime('2019-05-29') and timestamp < (toDateTime('2019-05-29') + interval 1 day)
限制 4
我已经编写了代码和 sql 查询以从数据库中获取数据:
sql_tables <- glue("
SELECT *
FROM mytable
LIMIT 4
")
table <- dbGetQuery(con, stri_encode(sql_tables, to = "UTF-8")) %>%
as.data.frame()
我得到这个数据框:
ID value timestamp
1 message sent 2019-05-29 06:45:34
2 sold out 2019-05-29 07:55:29
3 processed 2019-05-30 17:42:11
4 processed 2019-05-30 19:44:15
我想编写另一个查询以仅获取 2019-05-29 的数据:
sql_tables <- glue("
SELECT *
FROM mytable
WHERE timestamp = '2019-05-29'
LIMIT 4
")
table <- dbGetQuery(con, stri_encode(sql_tables, to = "UTF-8")) %>%
as.data.frame()
但它给我带来了一个错误:
Error in select(conn@ptr, statement) :
DB::Exception: Key expression contains comparison between inconvertible types: DateTime and String inside timestamp = '2019-05-29'
我该怎么做?我怎样才能摆脱 sql 查询中时间戳列中的小时数?期望的结果是:
ID value timestamp
1 message sent 2019-05-29
2 sold out 2019-05-29
来自manuals data types for date:
A date. Stored in two bytes as the number of days since 1970-01-01 (unsigned). Allows storing values from just after the beginning of the Unix Epoch to the upper threshold defined by a constant at the compilation stage (currently, this is until the year 2106, but the final fully-supported year is 2105).
The date value is stored without the time zone.
所以我们需要将"2019-05-29"
日期转换为数字:
as.numeric(as.Date("2019-05-29"))
# [1] 18045
#Origin is same as clickhouse, test:
as.Date(18045, origin = "1970-01-01")
# [1] "2019-05-29"
现在我们可以将其用作数字(未测试):
SELECT *
FROM mytable
WHERE timestamp = 18045
LIMIT 4
截止日期(时间戳)
SELECT * 从我的表 WHERE toDate(时间戳)='2019-05-29' 限制 4
SELECT * 从我的表 WHERE timestamp => toDateTime('2019-05-29') and timestamp < (toDateTime('2019-05-29') + interval 1 day) 限制 4