英文:
How to extract year, month or day from BIGINT epoch timestamp when inserting in Redshift
问题
从一个BIGINT时间戳1543258003796
在Redshift SQL环境中插入数据到表格时,我想提取一个日期格式,比如月份、年份或日期。
我有这样一个表格:
CREATE TABLE time_table (
month_date character varying(20),
year_date character varying(20)
);
现在,我想用来自另一个表格ts_table
的数据填充表格time_table
,后者有一个以BIGINT类型表示的时间戳列:
INSERT INTO time_table (month_date, year_date)
SELECT EXTRACT(month from ts.timestamp) as month_date,
EXTRACT(year from ts.timestamp) as year_date
FROM ts_table ts;
它会引发错误,因为ts.timestamp
是一个BIGINT。我应该首先将BIGINT转换为其他类型吗?或者有其他函数可以执行此操作吗?我尝试了几种方法,但仍然无法找到解决方案。
英文:
I would like to extract a date format like month, year or day from a BIGINT timestamp 1543258003796
in the Redshift SQL environment when inserting data into a table.
I have a table like this:
CREATE TABLE time_table (
month_date character varying(20),
year_date character varying(20),
);
Now I want to populate the table time_table
with data from another table ts_table
that has a column with timestamp as BIGINT type:
INSERT INTO time_table (month_date, year_date)
SELECT EXTRACT(month from ts.timestamp) as month_date,
EXTRACT(year from ts.timestamp) as year_date
FROM ts_table ts;
It raises an error because ts.timestamp
is a BIGINT. Should I first cast the BIGINT into something else? Or is there another function to perform this action? I tried several things but I am still not able to find a solution.
答案1
得分: 1
我假设这些BIGINT日期是纪元日期。所以你首先需要将其转换为时间戳 - 例如像这样:
select timestamp 'epoch' + t.timestamp * interval '1 second' AS timest
from ts_table t;
现在这不是你想要的,但它将其转换为时间戳数据类型并为你打开了可用的有用函数。
第二步是从中提取年份和月份。将它们组合在一起,你会得到:
WITH ts_conv as (
select timestamp 'epoch' + t.timestamp * interval '1 second' AS timest
from ts_table t
)
SELECT EXTRACT(month from ts.timest) as month_date,
EXTRACT(year from ts.timest) as year_date
FROM ts_conv ts;
当然,这可以嵌入到你的INSERT语句中。
英文:
I assume that these BIGINT dates are epoch dates. So you first need to convert this to a timestamp - for example like so:
select timestamp 'epoch' + t.timestamp * interval '1 second' AS timest
from ts_table t;
Now this isn't want you want but it gets you into a timestamp data type and opens up the useful functions available to you.
Step 2 is to EXTRACT the year and month from this. Putting these together you get:
WITH ts_conv as (
select timestamp 'epoch' + t.timestamp * interval '1 second' AS
timest
from ts_table t
)
SELECT EXTRACT(month from ts.timest) as month_date,
EXTRACT(year from ts.timest) as year_date
FROM ts_conv ts;
And this of course can be inside your INSERT statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论