Presto SQL-将字符串数据类型的日期转换为日期格式yyyy/mm/dd



ds:年月日。它以文本的形式存储,我们使用presto来运行。不需要日期函数

CREATE TABLE job_data(
ds DATE,
job_id INT NOT NULL,
actor_id INT NOT NULL,
event VARCHAR(15) NOT NULL,
language VARCHAR(15) NOT NULL,
time_spent INT NOT NULL,
org CHAR(2) );
INSERT INTO job_data (ds, job_id, actor_id, event, language, time_spent, org)
VALUES ('2020-11-30', 21, 1001, 'skip', 'English', 15, 'A'),
('2020-11-30', 22, 1006, 'transfer', 'Arabic', 25, 'B'),
('2020-11-29', 23, 1003, 'decision', 'Persian', 20, 'C'),
('2020-11-28', 23, 1005,'transfer', 'Persian', 22, 'D'),
('2020-11-28', 25, 1002, 'decision', 'Hindi', 11, 'B'),
('2020-11-27', 11, 1007, 'decision', 'French', 104, 'D'),
('2020-11-26', 23, 1004, 'skip', 'Persian', 56, 'A'),
('2020-11-25', 20, 1003, 'transfer', 'Italian', 45, 'C');

这里我使用了作为日期数据类型。

我代码:

select ds, count(job_id) as jobs_per_day, sum(time_spent)/3600 as hours_spent 
from job_data  
where ds >='2020-11-01'  and ds <='2020-11-30'  
group by ds ;

这就是我所做的,但我必须在字符串和I中输入日期我不明白我怎么能把它转换成日期不使用任何函数。我正在使用mysql工作台8.0.

如果没有函数,就不能将字符串转换为日期。在Presto中,您可以使用date_parse。试一试:

select cast(date_parse(ds,'%Y-%m-%d') as date) as the_date , 
count(job_id) as jobs_per_day, 
sum(time_spent)/3600 as hours_spent 
from job_data  
where the_date >='2020-11-01'  and the_date <='2020-11-30'  
group by the_date ;

相关内容

  • 没有找到相关文章

最新更新