【发布时间】: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