【问题标题】:Teradata SQL CASE Statement with multiple conditions具有多个条件的 Teradata SQL CASE 语句
【发布时间】:2019-05-22 11:33:33
【问题描述】:

我有以下 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

但我收到以下语法错误 - 语法错误,在 'AdmitDate' 和 ',' 之间应有类似“EXCEPT”关键字、“UNION”关键字或“MINUS”关键字

在外部选择语句中,我试图获取第一次诊断时的最小(第一个)日期。我还想只获取在 2013 年 6 月至 2018 年 6 月之间被诊断出的患者,这就是我有 CASE 声明的原因。但是 CASE 声明给了我错误。

【问题讨论】:

  • 只需删除order by AdmitDate 之后的分号。我不知道 Teradata 的 sql,但您甚至可能需要使用 order by 完全删除该行...

标签: sql case teradata


【解决方案1】:

正如@BarbarosÖzhan 已经写的那样,删除派生表中的最后一行order by AdmitDate;

但不需要 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;

我还切换到标准 SQL 日期文字 DATE '2013-06-01' 而不是 '06/01/2013'。前者 (DATE 'YYYY-MM-DD') 只有一种可能的格式,而后者取决于基列的格式,并且在它更改时可能会失败(当然不是在您的查询中,因为您在 CAST 中定义了它)。

【讨论】:

  • 非常感谢@dnoeth。修改后的代码效果很好。
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多