【发布时间】:2023-04-04 02:36:02
【问题描述】:
加入表
我有一个内连接的查询:
- 第一张桌子 (A)
- 第二张桌子 (B)
- 第三张表 (C)
排除 ID
我想排除行 满足(C)的某些条件从结果连接(A)和(B)。
我有如下查询,但我没有得到想要的结果。
任何有关从 (A x B) 中排除 (C) 中的 ID 的帮助将不胜感激。我想排除那些meds上的ID。
错误
我也收到一个错误:
expected 'UDFCALLNAME' keyword between ')' and ')'
Teradata 上的 SQL
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;
谢谢! JH
【问题讨论】:
-
我们怎么知道你想要什么?你没有解释你的逻辑,更不用说提供样本数据和期望的结果了。
-
使用 where 子句排除行
-
乍一看,您的 SQL 看起来过于复杂。为什么你有所有的内部
select * from表达式? -
您使用的是什么技术?此外,您永远不想选择 *,这是不好的做法。这太复杂了
-
我认为您确实需要在基本级别上了解内部连接,应该没有理由让这些内部选择继续进行。看看这篇文章,看看这是否对你有帮助。 sqlservertutorial.net/sql-server-basics/sql-server-inner-join 基本上,当您使用 INNER JOIN 时,您将拥有一个 ON 语句,它将正在连接的表链接到数据集中已有的表。然后,如果需要,您可以在此条件下添加其他 where 条件。
标签: sql syntax-error inner-join teradata