SQL查询 - 返回特定数据

时间:2013-09-10 14:48:25

标签: sql

我在AllScripts中运行查询,提供患者更新报告。我有我想要的所有列,除了一个之外的所有列都正确返回。另一种说法并非不正确,但它有许多值可以返回,而不是我想要的值。代码:

SELECT PT_BASIC.PATIENT_CODE, PT_BASIC.NAME_LAST, 
   PT_BASIC.NAME_FIRST, 
   PT_ADMISSION.ADMIT_DATE, 
   PT_ADMISSION.TERMINATION_DATE, 
   C_DIAGNOSIS.DIAGNOSIS, 
   PT_BASIC.DATE_OF_BIRTH, PT_BASIC.SEX, 
   PT_STATUS.STATUS_CODE, 
   O_DATASET.DATASET_NAME, 
   RES_BASIC.ORGANIZATION_NAME, 
   RES_BASIC_2.NAME_FULL, 
   PT_STATUS.STATUS_DATE, 
   RES_BASIC_3.NAME_FULL NAME_FULL_2, 
   RES_BASIC_3.NAME_FIRST NAME_FIRST_2, 
   RES_BASIC_3.NAME_LAST NAME_LAST_2, 
   C_DIAGNOSIS.ICD9_CODE, 
   RES_BASIC_4.NAME_FULL NAME_FULL_3, 
   A_ASSIGNMENT_TYPE.DESCRIPTION, 
   res_basic_3.name_last + ', ' + res_basic_3.name_first res_basic_3_name_last_res
FROM PT_BASIC PT_BASIC
  INNER JOIN PT_ADMISSION PT_ADMISSION ON 
 (PT_ADMISSION.PATIENT_ID = PT_BASIC.PATIENT_ID)
  INNER JOIN PT_STATUS PT_STATUS ON 
 (PT_STATUS.ADMISSION_ID = PT_ADMISSION.ADMISSION_ID)
  AND (PT_STATUS.PATIENT_ID = PT_ADMISSION.PATIENT_ID)
  INNER JOIN PTC_DIAGNOSIS PTC_DIAGNOSIS ON 
 (PTC_DIAGNOSIS.PT_DIAGNOSIS_ID = PT_STATUS.PRIMARY_DIAGNOSIS_ID)
  AND (PTC_DIAGNOSIS.PATIENT_ID = PT_STATUS.PATIENT_ID)
  INNER JOIN C_DIAGNOSIS C_DIAGNOSIS ON 
 (C_DIAGNOSIS.DIAGNOSIS_ID = PTC_DIAGNOSIS.DIAGNOSIS_ID)
  AND (C_DIAGNOSIS.DIAGNOSIS_SET_ID = PTC_DIAGNOSIS.DIAGNOSIS_SET_ID)
  INNER JOIN O_DATASET O_DATASET ON 
 (O_DATASET.DATASET_ID = PT_BASIC.DATASET_ID)
  LEFT OUTER JOIN PT_ADMISSION_REFERRAL PT_ADMISSION_REFERRAL ON 
 (PT_ADMISSION_REFERRAL.REFERRAL_ID = PT_BASIC.PATIENT_ID)
  FULL OUTER JOIN RES_BASIC RES_BASIC ON 
 (RES_BASIC.RESOURCE_ID = PT_ADMISSION.REFERRAL_SOURCE)
  FULL OUTER JOIN RES_BASIC RES_BASIC_2 ON 
 (RES_BASIC_2.RESOURCE_ID = PT_STATUS.ASSOCIATED_FACILITY_ID)
  FULL OUTER JOIN RES_BASIC RES_BASIC_3 ON 
 (RES_BASIC_3.RESOURCE_ID = PT_ADMISSION.PHYSICIAN_ID1)
  FULL OUTER JOIN PT_ASSIGNMENT PT_ASSIGNMENT ON 
 (PT_ASSIGNMENT.ASSIGNMENT_ID = PT_BASIC.PATIENT_ID)
  FULL OUTER JOIN A_ASSIGNMENT_TYPE A_ASSIGNMENT_TYPE ON 
 (A_ASSIGNMENT_TYPE.ADMIN_SET_ID = PT_ASSIGNMENT.ADMIN_SET_ID)
  AND (A_ASSIGNMENT_TYPE.TYPE_ID = PT_ASSIGNMENT.ASSIGNMENT_TYPE)
  FULL OUTER JOIN RES_BASIC RES_BASIC_4 ON 
 (RES_BASIC_4.RESOURCE_ID = PT_ASSIGNMENT.RESOURCE_ID)
WHERE 
  ( O_DATASET.DATASET_NAME = 'LIVE Seasons Hospice' )
   AND ( PT_ADMISSION.ADMIT_DATE > CONVERT(DATETIME,'2012-01-01',120) )
ORDER BY PT_STATUS.STATUS_DATE DESC, 
     PT_ADMISSION.ADMIT_DATE DESC, 
     PT_BASIC.PATIENT_CODE

我现在正在处理的部分是A_ASSIGNMENT_TYPE.DESCRIPTION。在每位患者中,都有一个分配清单。描述是分配类型,例如社会工作者或摄入护士。我只在寻找PCC。提取此信息的代码是:

 (PT_ASSIGNMENT.ASSIGNMENT_ID = PT_BASIC.PATIENT_ID)
  FULL OUTER JOIN A_ASSIGNMENT_TYPE A_ASSIGNMENT_TYPE ON 
 (A_ASSIGNMENT_TYPE.ADMIN_SET_ID = PT_ASSIGNMENT.ADMIN_SET_ID)
  AND (A_ASSIGNMENT_TYPE.TYPE_ID = PT_ASSIGNMENT.ASSIGNMENT_TYPE)
  FULL OUTER JOIN RES_BASIC RES_BASIC_4 ON 
 (RES_BASIC_4.RESOURCE_ID = PT_ASSIGNMENT.RESOURCE_ID)

现在,我的查询获得了正确数量的条目,与不查找分配的查询相比,它正在提取与该分配相关联的正确名称(res_basic_4),但它正在拉动我不需要的分配。我只希望它列出PCC的名称,即使资源名称为空(因为没有分配PCC)。

1 个答案:

答案 0 :(得分:0)

我不确定你的意思,但这就是你要找的东西吗?

FULL OUTER JOIN A_ASSIGNMENT_TYPE A_ASSIGNMENT_TYPE ON 
(A_ASSIGNMENT_TYPE.ADMIN_SET_ID = PT_ASSIGNMENT.ADMIN_SET_ID)
AND (A_ASSIGNMENT_TYPE.TYPE_ID = 'PCC' OR A_ASSIGNMENT_TYPE.TYPE_ID IS NULL)