【问题标题】:SQL Athena getting the data for the past 12 monthsSQL Athena 获取过去 12 个月的数据
【发布时间】:2020-08-13 11:43:53
【问题描述】:

我需要获取过去 12 个月的数据。我在 AWS Athena 中使用 SQL。以下是我的代码:

CREATE
OR REPLACE VIEW response_view AS
SELECT
    "cust"."customer_id",
    "cust"."event_triggered_date"
FROM
    (
        db.population_view pop
        INNER JOIN new_db.manual_response_view cust ON ("pop"."customer_id" = "cust"."customer_id")
    )
    WHERE "cust"."event_triggered_date" > current_date - interval '12' month

给我一​​个错误:cannot be applied to varchar, date

event_triggered_filed 是一个字符串

这是日期字段的结构:2019-12-04 00:00:00.000

【问题讨论】:

  • 这表明event_triggered_date 被存储为字符串。修复你的数据模型!
  • 感谢您的评论,我会更加感谢一些帮助 - 这就是我在 SO 上发布我的问题的原因。
  • EVENT_TRIGGERED_DATE的结构是什么?你能给我们一些样本数据吗? @Chique_Code
  • 使用提供的示例编辑了我的问题。

标签: sql amazon-web-services date amazon-athena


【解决方案1】:

试试这个。

CAST(EVENT_TRIGGERED_DATE AS DATE)

CAST(EVENT_TRIGGERED_DATE AS TIMESTAMP )

Data Types

【讨论】:

  • 我已经运行了您的代码并且查询成功,但是,一旦我尝试预览我创建的视图,我就会遇到以下错误:INVALID_CAST_ARGUMENT: Value cannot be cast to date: 2019-10-17 00:00:00.000
  • 编辑了我的答案,请检查。 @Chique_Code
  • 非常感谢! @Chique_Code
【解决方案2】:

我有同样的问题,我需要从当前日期获得第 6 个月和第 12 个月的声明日期。

我已经写了这个,它适用于我,我希望它也适用于其他人。

查询结果请参考附图

select current_date as today, date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Start_of_the_date_6_month_before

日期前 12 个月的起始日。

select current_date as today, date_add('month', -12, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Starting_day_of_the_month_12_before

最后我做出这样的改变:

With
six_month_And_minus_1_day AS (
select date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01')))) as Start_of_the_date_6_month_before,
date_add('day', -1,  date_add('month', -6, (select date(date_format(cast(current_date as date),'%Y-%m-01'))))) as Start_of_the_date_6_month_before_minus_1
)
select * from six_month_And_minus_1_day

请任何人有更好的方法来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多