使用COUNT选择DISTINCT

时间:2013-04-03 09:58:38

标签: sql count distinct

我想计算目前未被录取的患者人数(CurrentClinicalInfo = 'False')。但是,每位患者有多个ClinicalInfo s。因此我需要有所区别。

我在下面尝试了这个代码,但是它附带了太多列导致错误。

 SELECT DISTINCT COUNT(*) AS TableLength
    FROM            PatientDemographics AS p LEFT OUTER JOIN
                             PatientClinicalinformation AS pc ON p.PatientID = pc.PatientID
    WHERE        (pc.PatientID IS NULL) AND (p.FirstName LIKE '%' + + '%') OR
                             (pc.PatientID IS NULL) AND (p.Surname LIKE '%' + + '%') OR
                             (p.FirstName LIKE '%' + + '%') AND (pc.CurrentClinicalInfo = 'False') OR
                             (p.Surname LIKE '%' + + '%
    ') AND (pc.CurrentClinicalInfo = 'False')

1 个答案:

答案 0 :(得分:1)

你想要不同的患者,对吗?

SELECT COUNT(DISTINCT p.PatientID) AS TableLength
FROM PatientDemographics AS p
LEFT JOIN PatientClinicalinformation AS pc ON p.PatientID = pc.PatientID
WHERE pc.PatientID IS NULL AND p.FirstName LIKE '%' + @input + '%'
      OR
      pc.PatientID IS NULL AND p.Surname LIKE '%' + @input + '%'
      OR
      p.FirstName LIKE '%' + @input + '%' AND pc.CurrentClinicalInfo = 'False'
      OR
      p.Surname LIKE '%' + @input + '%' AND pc.CurrentClinicalInfo = 'False'