【问题标题】:Timestamps differences using AWS athena使用 AWS athena 的时间戳差异
【发布时间】:2020-01-29 16:28:21
【问题描述】:

我正在我的 jupyter 笔记本中使用 AWS Athena 运行 SQL 查询,如下所示。它涉及计算时间戳之间的差异,如下所示。

query_demog = """


select ad.subject_id, ad.hadm_id, i.icustay_id ,
       date_diff('second', timestamp '1970-01-01 00:00:00', ad.admittime) as admittime,
       date_diff('second', timestamp '1970-01-01 00:00:00', ad.dischtime) as dischtime,
       ROW_NUMBER() over (partition by ad.subject_id order by i.intime asc) as adm_order,
       case when i.first_careunit='NICU' then 5
            when i.first_careunit='SICU' then 2
            when i.first_careunit='CSRU' then 4
            when i.first_careunit='CCU' then 6
            when i.first_careunit='MICU' then 1
            when i.first_careunit='TSICU' then 3
       end as unit,
       date_diff('second', timestamp '1970-01-01 00:00:00', i.intime) as intime,
       date_diff('second', timestamp '1970-01-01 00:00:00', i.outtime) as outtime,
       i.los,
from mimiciii.admissions ad,
     mimiciii.icustays i,
     mimiciii.patients p
where ad.hadm_id=i.hadm_id and p.subject_id=i.subject_id 
order by subject_id asc, intime asc

"""

它工作正常。现在,当我包含具有相似时间戳差异的另一行时,我得到一个错误。

query_demog = """


select ad.subject_id, ad.hadm_id, i.icustay_id ,date_diff('second', timestamp '1970-01-01 00:00:00', ad.admittime) as admittime, date_diff('second', timestamp '1970-01-01 00:00:00', ad.dischtime) as dischtime, ROW_NUMBER() over (partition by ad.subject_id order by i.intime asc) as adm_order, case when i.first_careunit='NICU' then 5 when i.first_careunit='SICU' then 2 when i.first_careunit='CSRU' then 4 when i.first_careunit='CCU' then 6 when i.first_careunit='MICU' then 1 when i.first_careunit='TSICU' then 3 end as unit,  date_diff('second', timestamp '1970-01-01 00:00:00', i.intime) as intime, date_diff('second', timestamp '1970-01-01 00:00:00', i.outtime) as outtime, i.los,

 EXTRACT(EPOCH FROM (i.intime-p.dob)::INTERVAL)/86400 as age





from mimiciii.admissions ad, mimiciii.icustays i, mimiciii.patients p

where ad.hadm_id=i.hadm_id and p.subject_id=i.subject_id 

order by subject_id asc, intime asc

"""

包含EXTRACT(EPOCH FROM (i.intime-p.dob)::INTERVAL)/86400 as age 行会产生如下错误。

调用时发生错误(InvalidRequestException) StartQueryExecution 操作:第 3:37 行:输入不匹配 ':' 期待 {'.', ')', '[', 'AT', '+', '-', '*', '/', '%', '||'} 无法 回滚

我不知道为什么包含 EXTRACT(EPOCH FROM (i.intime-p.dob)::INTERVAL)/86400 as age 会产生错误

【问题讨论】:

  • 今日提示:始终使用现代、明确的JOIN 语法。更容易编写(没有错误),更容易阅读(和维护),并且在需要时更容易转换为外连接。

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


【解决方案1】:

to_unixtime() 内置应该可以工作:

to_unixtime(i.intime-p.dob)/86400 as age

【讨论】:

  • 间隔呢?
  • @AbdulKarimKhan 你能详细说明间隔是什么意思吗?
  • EXTRACT(EPOCH FROM (i.intime-p.dob)::INTERVAL)/86400 as age 这包含INTERVAL。是不是觉得INTERVAL在这行代码中有一些特定的功能。
  • @AbdulKarimKhan 在这种情况下,由于您关心以秒为单位的差异,因此据我所知,to_unixtime() 似乎不需要INTERVAL,就像::INTERVAL一种 Postgres 方言,意思是“将差异转换为类型 INTERVAL”。
猜你喜欢
  • 2012-06-03
  • 2015-06-27
  • 2021-02-02
  • 1970-01-01
  • 2018-09-27
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多