gpt4 book ai didi

sql - Date_Trunc 无法按预期工作

转载 作者:行者123 更新时间:2023-12-02 20:00:29 29 4
gpt4 key购买 nike

我试图在 SQL 语句中使用 Date_Trunc for MONTH 函数,但不知何故它对我不起作用。我正在尝试提取 2019 年 4 月 1 日之后发生的条目。Redshift 数据库中的原始日期格式是我尝试将其分组为月/年存储桶的格式:2019-04-08T00:13:20.000Z

输入

SELECT
client_id as user_id,
session_utc as job_date --(format:2019-04-08T00:13:20.000Z)
FROM table1 as hits
WHERE job_date >= DATE_TRUNC('month', 2019-04-01)
group by 1,2;

输出

"ERROR: function date_trunc("unknown", integer) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts."

我做错了什么?

最佳答案

DATE_TRUNC Function - Amazon Redshift时间戳作为输入并提供时间戳作为输出:

DATE_TRUNC('datepart', timestamp)

例如:

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)

2019-05-01 00:00:00

因此,您的行应为:

WHERE job_date >= DATE_TRUNC('month', '2019-04-01'::timestamp) 

如果您希望输出为日期,请附加 ::date:

SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)::date

2019-05-01

另请注意,日期 转换为午夜时的时间戳。这可能会导致某些比较出现差异。例如:

'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp

将评估为True,因为它是与一天开始时的午夜进行比较。这与比较两个日期(没有时间戳)不同。

关于sql - Date_Trunc 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56032125/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com