Teradata SQL排除错误

时间:2019-02-27 23:28:37

标签: sql teradata

由于原始问题尚不清楚,因此我将重新发布查询。我需要从前2个表中排除meds表中的ID。我得到一个类似名称或Unicode分隔或')'和')'错误之间的'UDFCALLNAME'关键字的预期错误。

select distinct first_visit.pat_id, 
            first_visit.PatientMRN, 
            first_visit.PatientName, 
            cast(contact_date as  date format 'yyyy-mm-dd') as visit_date, 
            first_visit.hsp_account_id as hsp_acct
from            
(
SELECT  distinct pat.pat_id,
    pat.PAT_MRN_ID as PatientMRN,
    pat.PAT_NAME as PatientName,
    contact_date,
    cast(pat.Birth_Date as  date format 'yyyy-mm-dd') as DOB,
    cast(contact_date as date) - cast( pat.birth_date as date) year (4) AS PatAge,
   enc.enc_type_c,
   dept.specialty,
   dept.department_name,
    specName.NAME,
    prov_name,
    acct.hsp_account_id     
FROM  Patient pat
inner JOIN  HSP_ACCOUNT acct on  acct.PAT_ID = pat.PAT_ID 
inner JOIN  Pat_enc enc on  enc.PAT_ID = pat.PAT_ID 
inner join clarity_ser_dept ser  on ser.prov_id = enc.visit_prov_id
inner join clarity_ser prov on prov.prov_id = ser.prov_id 
inner join CLARITY_SER_SPEC spec on enc.visit_prov_id = spec.PROV_ID 
inner join ZC_SPECIALTY specName on specName.SPECIALTY_C = spec.SPECIALTY_C and spec.LINE=1 and specName.NAME = 'xxx'
inner join CLARITY_DEP Dept on Dept.DEPARTMENT_ID = enc.DEPARTMENT_ID  and dept.specialty = specname.name
inner join CLARITY_LOC provLoc on provLoc.LOC_ID = Dept.REV_LOC_ID
where patAge > 55
and cast(contact_date as  date format 'mm/dd/yyyy') >= '10/01/2018'
and ACCT_CLASS_HA_C  =  '1207'
and acct.CODING_STATUS_C = 4 
and acct.ACCT_BILLSTS_HA_C NOT IN (40, 60, 99) 
AND SUBSTR(CAST (acct.HSP_ACCOUNT_ID AS VARCHAR(18)),4,1) IN ('1','3')
and enc_type_c  = '101'
group by 1,2,3,4,5,6,7,8,9,10,11,12
qualify row_number() over (partition by PatientMRN  order by contact_date DESC) =1) first_visit


inner join 
(select problem.pat_ID, edg.DX_ID, noted_date
from problem_list problem
inner join clarity_edg edg on problem.DX_ID = edg.DX_ID  
--where pat_id = '212000001293964'
and DX_name like any ('%Atrial FLutter%', '%Atrial Fib%')
) DX on DX.pat_id = first_visit.pat_id and DX.noted_date < first_visit.contact_date

/* exclude */
left  join 
(select distinct pat_id 
from (select pat_id from order_med ordmed
inner join RX_MED_TWO rxmed  on ordmed.medication_id = rxmed.medication_id 
inner join ZC_ADMIN_ROUTE  zc on ADMIN_ROUTE_C = zc.med_route_c 
where medication_name  like any ( '%Coumadin%',  '%Eliquis%'))) meds
on first_visit.pat_id = meds.pat_id 
where meds.pat_id is null;  

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

Teardata需要所有派生表的别名。 请在qualify row_number()

之前添加一些别名

以下是查询:

select distinct first_visit.pat_id, 
            first_visit.PatientMRN, 
            first_visit.PatientName, 
            cast(contact_date as  date format 'yyyy-mm-dd') as visit_date, 
            first_visit.hsp_account_id as hsp_acct
from            
(
SELECT  distinct pat.pat_id,
    pat.PAT_MRN_ID as PatientMRN,
    pat.PAT_NAME as PatientName,
    contact_date,
    cast(pat.Birth_Date as  date format 'yyyy-mm-dd') as DOB,
    cast(contact_date as date) - cast( pat.birth_date as date) year (4) AS PatAge,
   enc.enc_type_c,
   dept.specialty,
   dept.department_name,
    specName.NAME,
    prov_name,
    acct.hsp_account_id     
FROM  Patient pat
inner JOIN  HSP_ACCOUNT acct on  acct.PAT_ID = pat.PAT_ID 
inner JOIN  Pat_enc enc on  enc.PAT_ID = pat.PAT_ID 
inner join clarity_ser_dept ser  on ser.prov_id = enc.visit_prov_id
inner join clarity_ser prov on prov.prov_id = ser.prov_id 
inner join CLARITY_SER_SPEC spec on enc.visit_prov_id = spec.PROV_ID 
inner join ZC_SPECIALTY specName on specName.SPECIALTY_C = spec.SPECIALTY_C and spec.LINE=1 and specName.NAME = 'xxx'
inner join CLARITY_DEP Dept on Dept.DEPARTMENT_ID = enc.DEPARTMENT_ID  and dept.specialty = specname.name
inner join CLARITY_LOC provLoc on provLoc.LOC_ID = Dept.REV_LOC_ID
where patAge > 55
and cast(contact_date as  date format 'mm/dd/yyyy') >= '10/01/2018'
and ACCT_CLASS_HA_C  =  '1207'
and acct.CODING_STATUS_C = 4 
and acct.ACCT_BILLSTS_HA_C NOT IN (40, 60, 99) 
AND SUBSTR(CAST (acct.HSP_ACCOUNT_ID AS VARCHAR(18)),4,1) IN ('1','3')
and enc_type_c  = '101'
group by 1,2,3,4,5,6,7,8,9,10,11,12) Blah
qualify row_number() over (partition by PatientMRN  order by contact_date DESC) =1) first_visit


inner join 
(select problem.pat_ID, edg.DX_ID, noted_date
from problem_list problem
inner join clarity_edg edg on problem.DX_ID = edg.DX_ID  
--where pat_id = '212000001293964'
and DX_name like any ('%Atrial FLutter%', '%Atrial Fib%')
) DX on DX.pat_id = first_visit.pat_id and DX.noted_date < first_visit.contact_date

/* exclude */
left  join 
(select distinct pat_id 
from (select pat_id from order_med ordmed
inner join RX_MED_TWO rxmed  on ordmed.medication_id = rxmed.medication_id 
inner join ZC_ADMIN_ROUTE  zc on ADMIN_ROUTE_C = zc.med_route_c 
where medication_name  like any ( '%Coumadin%',  '%Eliquis%'))) meds
on first_visit.pat_id = meds.pat_id 
where meds.pat_id is null;  

答案 1 :(得分:0)

如果您设置查询格式,则查找问题要容易得多

LEFT  JOIN 
 (
   SELECT DISTINCT pat_id 
   FROM
    ( SELECT pat_id
      FROM order_med ordmed
      INNER JOIN RX_MED_TWO rxmed  ON ordmed.medication_id = rxmed.medication_id 
      INNER JOIN ZC_ADMIN_ROUTE  zc ON ADMIN_ROUTE_C = zc.med_route_c 
      WHERE medication_name  LIKE ANY ( '%Coumadin%',  '%Eliquis%')
    ) ----- MISSING ALIAS
 ) meds
ON first_visit.pat_id = meds.pat_id 
WHERE meds.pat_id IS NULL;

当然,您根本不需要嵌套的派生表:

LEFT  JOIN 
 (
   SELECT DISTINCT pat_id 
   FROM order_med ordmed
   INNER JOIN RX_MED_TWO rxmed  ON ordmed.medication_id = rxmed.medication_id 
   INNER JOIN ZC_ADMIN_ROUTE  zc ON ADMIN_ROUTE_C = zc.med_route_c 
   WHERE medication_name  LIKE ANY ( '%Coumadin%',  '%Eliquis%')
 ) meds
ON first_visit.pat_id = meds.pat_id 
WHERE meds.pat_id IS NULL;

顺便说一句,在使用.NET连接时,Teradata Studio将在提交之前向您显示错误的语法,并且SQL Assistant跳转到错误的位置。