【问题标题】:stuck on date function in Presto SQL卡在 Presto SQL 中的日期函数上
【发布时间】:2019-05-20 06:11:59
【问题描述】:

我的代码中的日期函数卡住了:

try(date_parse(min(MilestoneDate), '%Y-%m-%d %H:%i:%s')) >  CURRENT_DATE

消息错误是:一般内部错误。我不知道这意味着什么。 如果我注释掉 'try(date_parse(min(MilestoneDate), '%Y-%m-%d %H:%i:%s')) > CURRENT_DATE',则整个代码都有效。但我真的需要它基于要求。我也尝试:拥有 DATE(SUBSTR(min(finishdate), 1, 10)) > CURRENT_DATE。但错误消息是:无法投射到日期。请帮忙。谢谢。

With ManagementView1 as (

select * from Management_View a


left join


(select *
 from

(select projectobjectid, id as activity_id,finishdate as MilestoneDate, name as Milestone
 from activity 
 where date = (select max(date) from activity)
 union ALL 
 select projectobjectid, id as activity_id, min(finishdate) as finishdate, 
 name
 from activity 
 where id in ('FS1000', 'PR1000', 'PR1500')

 group by projectobjectid, id, name)
) b

ON  try_cast(a.objectid as double) = b.projectobjectid 
 AND a.id = b.activity_id 
)    

select * from

    (
        select site, building, id, milestonetype, MilestoneDate, Milestone 
         from ManagementView1

         WHERE  milestonetype in ('Breakground', 'Energization')

        UNION ALL 

       select site, building, id, milestonetype, min(MilestoneDate) as 
        MilestoneDate,  Milestone

        from ManagementView1 

        where milestonetype = 'PR'

       having try(date_parse(min(MilestoneDate), '%Y-%m-%d %H:%i:%s')) > 
       CURRENT_DATE


       --having DATE(SUBSTR(min(finishdate), 1, 10)) > CURRENT_DATE

         -- and milestonetype = 'PR'
        group by site, building, id, milestonetype, Milestone


    ) c

【问题讨论】:

  • 不起作用的值是什么样的?为什么要将日期/时间值存储为字符串?
  • 我尝试了您的解决方案,并且我还稍微修改了 sql。现在可以了。但是还有另一个问题。我从查询中获得的数据只是“UNION ALL”上方第一部分的数据(那些属于“Breakground”和“Energization”的数据)。我无法从“UNION ALL”下方的第二部分获取数据。以下是修改后的查询:
  • “一般内部错误。” -- 实际的消息和/或堆栈跟踪是什么?

标签: sql presto


【解决方案1】:

我认为try() 不能与date_parse() 一起使用——即使可以,它也不会捕获所有错误。但它应该适用于cast()。所以,试试这个:

DATE(SUBSTR(MIN(finishdate), 1, 10))

MIN(TRY(CAST(SUBSTR(MilestoneDate, 1, 10) as date))) > CURRENT_DATE

【讨论】:

  • 我尝试了您的解决方案。现在可以了。但是还有另一个问题。我从查询中得到的数据只是 'UNION ALL' 上面第一部分的数据。我无法从“UNION ALL”下面的第二部分获取数据。
  • select * from (select site, building, id, landmarktype, MilestoneDate, Milestone from ManagementView1 WHERE landmarktype in('Breakground''Energization') UNION ALL select site, building, id, landmarktype, min( MilestoneDate) 作为 MilestoneDate,来自 ManagementView1 的里程碑,其中里程碑类型 = 'PR' 按站点、建筑物、id、里程碑类型分组,里程碑具有 min(TRY(cast(Substr(milestoneDate, 1, 10) as DATE))) > CURRENT_DAT) c
  • 我用过:trim(MilestoneType) = 'PR'。现在可以了。但问题是我想在下面的“UNION ALL”(PR 类别)中获得 min(MilestoneDate),而且 min(MilestoneDate) 必须大于今天,并且最接近今天。现在,我仍然在 PR 类别中获得多个 MilestoneDates。有什么解决方案可以得到这个结果吗?谢谢。
  • try 与函数一起使用与casts (prestosql.io/docs/current/functions/conditional.html#try) 一样。事实上,try_cast 只是try(cast(...)) 的语法糖。但是,按照设计 try 并没有捕捉到所有可能的故障情况,因此它也有可能没有捕捉到一些应该捕捉到的故障。
  • @南希。 . .这个问题是关于您尝试解析日期的。如果您还有其他问题,请将其作为问题提出。
猜你喜欢
  • 2019-09-30
  • 2021-08-31
  • 2021-10-07
  • 2020-04-04
  • 2017-01-18
  • 2018-02-16
  • 2019-11-20
  • 1970-01-01
  • 2019-02-09
相关资源
最近更新 更多