亚马逊雅典娜时间日期字符串列

628mspwn  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(402)

athena分区是按年/月/日来划分的,并通过glue作为字符串列导入。因此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

这是一天。我要从现在到过去的7天。

p1tboqfb

p1tboqfb1#

我通过创建最后七天的序列并从这些值中提取年、月和日来实现这一点。我的雅典娜数据中的年/月/日列存储为整数,因此我不需要强制转换 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;
camsedfj

camsedfj2#

我想是在处理presto文档 date_parse 这是一个更简单的方法。以下是选择的步骤 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]

相关问题