多选或不同

时间:2013-12-12 00:24:29

标签: sql

我是sql的新手,所以寻求一些帮助 - 第一部分失败然而我遇到了第二部分的问题。

我把三张桌子捆在了一起。首先,我需要将tblPatient.ID = tblPatientVisit.PatientID绑定在一起,以消除可行的重复

现在我需要获取这些结果并消除MRN中的重复但我的查询只返回一个错误的结果 - LOL

查询

select 
      tblPatient.id, 
      tblPatient.firstname, 
      tblPatient.lastname, 
      tblPatient.dob, 
      tblPatient.mrn, 
      tblPatientSmokingScreenOrder.SmokeStatus, 
      tblPatientVisit.VisitNo 
   from
      tblPatient, 
      tblPatientSmokingScreenOrder, 
      tblPatientVisit
   Where 
      tblPatient.ID = tblPatientVisit.PatientID 
      and tblPatientVisit.ID = tblPatientSmokingScreenOrder.VisitID
      and tblPatient.ID in(
         Select Distinct 
            tblPatient.mrn
         From 
            tblPatient
         where
            isdate(DOB) = 1 
            and Convert(date,DOB) <'12/10/2000' 
            and tblPatientVisit.PatientType = 'I')

实际结果:

ID | firstName  | LastName  | DOB        | MRN     | SmokeStatus  | VisitNO
12 | Test Guy   | Today     | 12/12/1023 | 0015396 | Never Smoker | 0013957431

期望的结果:

90 | BOB        | BUILDER   | 02/24/1974 | 0015476 | Former Smoker | 0015476001
77 | DORA       | EXPLORER  | 06/04/1929 | 0015463 | Never Smoker  | 0015463001
76 | MELODY     | VALENTINE | 09/17/1954 | 0015461 | Current       | 0015461001
32 | STRAWBERRY | SHORTCAKE | 07/06/1945 | 0015415 | Current       | 0015415001
32 | STRAWBERRY | SHORTCAKE | 07/06/1945 | 0015415 | Never Smoker  | 0015415001
32 | STRAWBERRY | SHORTCAKE | 07/06/1945 | 0015415 | Former Smoker | 0015415001
12 | Test Guy   | Today     | 12/12/1023 | 0015345 | Never Smoker  | 0013957431

任何人都对我如何进入下一级别并获得具有一个唯一MRN的所有行有任何建议。根据上面的数据,我的列表中应该有5个。任何帮助将不胜感激。

由于

3 个答案:

答案 0 :(得分:0)

如果我不得不猜测 - 唯一看起来奇怪的东西(但也许没关系)就是你将父查询中的patient.ID与子查询中的patient.mrn进行比较。

除此之外 - 要检查的事情:

(1) 您是否通过内部询问得到了所有患者?

Select Distinct 
   tblPatient.mrn
From 
   tblPatient
where
   isdate(DOB) = 1 
   and Convert(date,DOB) <'12/10/2000' 
   and tblPatientVisit.PatientType = 'I'

(2) 丢失记录的患者类型是什么? (您将其过滤到tblPatientVisit.PatientType = 'I' - 缺失的记录是否也包含患者类型?)

答案 1 :(得分:0)

也许你需要在这里反转逻辑。如,

Select Distinct 
   patients.mrn1
From (select 
         tblPatient.id as id1, 
         tblPatient.firstname as firstname1, 
         tblPatient.lastname as lastname1, 
         tblPatient.dob as DOB1, 
         tblPatient.mrn as mrn1, 
         tblPatientSmokingScreenOrder.SmokeStatus as SmokeStatus1, 
         tblPatientVisit.VisitNo as VisitNo1, 
         tblPatientVisit.PatientType as PatientType1,
      from
         tblPatient, 
         tblPatientSmokingScreenOrder, 
         tblPatientVisit
      Where 
         tblPatient.ID = tblPatientVisit.PatientID 
         and tblPatientVisit.ID = tblPatientSmokingScreenOrder.VisitID
   ) as patients
where
   isdate(patients.DOB1) = 1 
   and Convert(date,patients.DOB1) <'12/10/2000' 
   and patients.PatientType1 = 'I');

答案 2 :(得分:0)

清理了一下,我认为他们是对的。 MRN不会与患者ID匹配,至少不会与您的示例数据匹配。您不应该需要内部查询。此查询应该为您提供所需的内容。

SELECT DISTINCT 
      p.id, 
      p.firstname, 
      p.lastname, 
      p.dob, 
      p.mrn, 
      s.SmokeStatus, 
      v.VisitNo 
 FROM
      tblPatient p 
      JOIN tblPatientVisit v ON p.id = v.patientId
      JOIN tblPatientSmokingScreenOrder s ON v.id = s.visitId  
 WHERE 
      isdate(p.DOB) = 1 
      AND CONVERT(date,p.DOB) <'12/10/2000' 
      AND v.PatientType = 'I'