[英]Teradata SQL CASE Statement with multiple conditions
I have the below SQL query - 我有以下SQL查询-
select distinct HospitalAcctID,
AdmitDate,
DischargeDate,
PatMRN,
Pat_id,
ICD,
MedCenter,
(case when SeqCount =1 and AdmitDate > '06/01/2013' and AdmitDate < '06/01/2018' then 1 else null end ) Firstdiag
from
(
select distinct acct.HSP_ACCOUNT_ID as HospitalAcctID,
cast(acct.ADM_DATE_TIME as date format 'mm/dd/yyyy') as AdmitDate,
cast(acct.DISCH_DATE_TIME as date format 'mm/dd/yyyy') as DischargeDate,
pat.pat_mrn_id as PatMRN,
pat.pat_id as Pat_id,
REF_BILL_CODE as ICD,
grp7.NAME AS MedCenter,
row_number() over (partition by PatMRN order by AdmitDate) as SeqCount
from acct
inner join pat on pat.pat_id = acct.pat_id
inner join hspenc on hspenc.CSN_ID = acct.CSN_ID
inner join dx on acct.ACCOUNT_ID = dx.ACCOUNT_ID and line = 1
inner join edg on dx.DX_ID = edg.DX_ID
inner join loc on loc.LOC_ID = acct.LOC_ID
inner join grp7 ON loc.RPT_GRP_SEVEN = grp7.RPT_GRP_SEVEN
where
grp7.NAME = 'SMC AREA'
and ADMIT_CONF_STAT_C in ('1','4')
and (edg. REF_BILL_CODE in ('431',
'431')
)
and ADT_PAT_CLASS_C in ('1204','12113')
order by AdmitDate;
)Admit
But I am getting the below syntax error - Syntax error, expected something like an 'EXCEPT' keyword, 'UNION' Keyword or a 'MINUS' keyword between 'AdmitDate' and ',' 但是我遇到了以下语法错误-语法错误,预期是“ AdmitDate”和“,”之间的“ EXCEPT”关键字,“ UNION”关键字或“ MINUS”关键字
In the outer select statement, I am trying to get the min (first ) date when the was first diagnosed. 在外部的select语句中,我试图获取首次诊断出的最小(第一个)日期。 I also want to get only the patients who were diagnosed between 6/2013 to 6/2018 which is why I have the CASE statement.
我也只想获取在6/2013到6/2018之间被诊断出的患者,这就是为什么要使用CASE语句。 But the CASE statement is giving me error.
但是CASE语句给了我错误。
As @BarbarosÖzhan already wrote, remove the last line order by AdmitDate;
正如@BarbarosÖzhan所写,删除
order by AdmitDate;
的最后一行order by AdmitDate;
in the Derived Table. 在派生表中。
But there's no need for ROW_NUMBER: 但是不需要ROW_NUMBER:
select distinct acct.HSP_ACCOUNT_ID as HospitalAcctID,
cast(acct.ADM_DATE_TIME as date format 'mm/dd/yyyy') as AdmitDate,
cast(acct.DISCH_DATE_TIME as date format 'mm/dd/yyyy') as DischargeDate,
pat.pat_mrn_id as PatMRN,
pat.pat_id as Pat_id,
REF_BILL_CODE as ICD,
grp7.NAME AS MedCenter,
case when -- current rows is first row
min(AdmitDate)
over (partition by PatMRN) = AdminDate
-- current row within date range
and AdminDate >= DATE '2013-06-01' and AdmitDate < DATE '2018-06-01'
then 1
else null
end as Firstdiag
from acct
inner join pat on pat.pat_id = acct.pat_id
inner join hspenc on hspenc.CSN_ID = acct.CSN_ID
inner join dx on acct.ACCOUNT_ID = dx.ACCOUNT_ID and line = 1
inner join edg on dx.DX_ID = edg.DX_ID
inner join loc on loc.LOC_ID = acct.LOC_ID
inner join grp7 ON loc.RPT_GRP_SEVEN = grp7.RPT_GRP_SEVEN
where
grp7.NAME = 'SMC AREA'
and ADMIT_CONF_STAT_C in ('1','4')
and (edg. REF_BILL_CODE in ('431',
'431')
)
and ADT_PAT_CLASS_C in ('1204','12113')
order by AdmitDate;
I also switched to a Standard SQL date literal DATE '2013-06-01'
instead of '06/01/2013'
. 我还切换到了标准SQL日期文字
DATE '2013-06-01'
而不是'06/01/2013'
。 There's only one possible format for the former ( DATE 'YYYY-MM-DD'
) while the latter depends on the FORMAT of the base column and might fail when it changes (of course not in your query, because you defined it in the CAST). 前者只有一种可能的格式(
DATE 'YYYY-MM-DD'
),而后者则取决于基本列的FORMAT,并且在其更改时可能会失败(当然不是在您的查询中,因为您在CAST中定义了它) )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.