Amazon Athena 时间日期作为字符串列
Posted
技术标签:
【中文标题】Amazon Athena 时间日期作为字符串列【英文标题】:Amazon Athena time date as string column 【发布时间】:2018-09-14 10:00:39 【问题描述】:Athena 分区按年/月/日划分,并由 GLUE 作为 STRING 列导入。因此,day 是一个类型字符串。我的要求是从当前时间戳中提取日期并与我的日期列/分区进行比较。
SELECT *
FROM "db1"."tbl1"
WHERE year = cast(extract(year from (CURRENT_DATE - interval '7' day)) as varchar)
AND month = lpad(cast(extract(month from (CURRENT_DATE - interval '7' day)) as varchar),2,'0')
AND day = lpad(cast(extract(day from (CURRENT_DATE - interval '7' day)) as varchar),2,'0')
limit 10
这会提取 single day 。我想要从现在到过去 7 天之间的所有天数。
【问题讨论】:
【参考方案1】:我通过创建过去 7 天的序列并从这些值中提取年、月和日来解决这个问题。我的 Athena 数据中的年/月/日列存储为整数,因此我不需要转换 EXTRACT
函数的结果,但我在此答案中转换结果以匹配您的用例。
-- there may be a more elegant approach, but this worked for me
-- create a CTE containing the last seven dates
WITH dates AS (
SELECT
date_add('day', n, current_date) AS date
FROM (
SELECT
ROW_NUMBER() OVER ()-7 AS n
FROM db1.tbl1
LIMIT 7
)
),
-- extract the year, month, and day for joining
date_parts AS (
SELECT
CAST(EXTRACT(YEAR FROM date) AS VARCHAR) AS year,
CAST(EXTRACT(MONTH FROM date) AS VARCHAR) AS month,
CAST(EXTRACT(DAY FROM date) AS VARCHAR) AS day
FROM dates
)
-- return all results from the last seven days
SELECT
*
FROM date_parts AS dp
JOIN db1.tbl1 AS t1 ON dp.year = t1.year AND dp.month = t1.month AND dp.day = t1.day;
【讨论】:
【参考方案2】:通过我认为 date_parse
的 presto 文档工作是一种更简单的方法。以下是步骤
选择current_date
CONCAT concat(年、月、日)
然后date_parse
cast(date_parse(concat(year, month, day), '%Y%m%d') as date) BETWEEN current_date - interval '7' day AND current_date
[https://prestodb.io/docs/current/functions/datetime.html][1]
【讨论】:
以上是关于Amazon Athena 时间日期作为字符串列的主要内容,如果未能解决你的问题,请参考以下文章