获取具有多个字段值实例的记录

时间:2014-04-21 20:25:15

标签: sql-server self-join

我有一个患者数据库,我正在尝试提取具有特定诊断并执行特定程序的患者列表。问题是我只想检索一个至少完成两次手术的患者列表。这是一些示例数据。

Table Name = "ClinicalData"

PatientID        Diagnosis           Procedure               ProcedureDate
--------------------------------------------------------------------------
1                Laceration          Stitches                2/12/2013
1                Fracture            Cast                    2/12/2013
1                Fracture            Cast                    2/13/2013
2                Lung-Cancer         Chemotherapy            4/07/2013
2                Lung-Cancer         Radiation               3/02/2013
2                Liver-Cancer        Chemotherapy            6/03/2013
3                Diabetes            Hemoglobin-A1C-Check    3/12/2013
3                Diabetes            Hemoglobin-A1C-Check    7/11/2013

SELECT 
   PatientID,
   Min(ProcedureDate) 
FROM 
   ClinicalData cd1
      INNER JOIN ClinicalData cd2 ON cd1.PatientID = PatientID
         AND Diagnosis IN ('Laceration', 'Lung-Cancer','Diabetes')
WHERE
   Procedure IN ('Stitches', 'Cast', 'Hemoglobin-A1C-Check')
GROUP BY
   PatientID

我想要获得的是所有诊断为(Laceration, Lung-Cancer, or Diabetes)的患者的清单,这些患者已经列出了至少两次所列程序之一('Stitches', 'Cast', 'Hemoglobin-A1C-Check')及其最早的访问日期。所以在上面的例子中我想获得......

PatientID       ProcedureDate
------------------------------
3               3/12/2013

因为我只感兴趣找Laceration, Lung-Cancer, and Diabetes通知Patient 1因为他/她的诊断为Fracture而未包括WHERE的患者。我遇到的问题是如何让那些符合我正在寻找的诊断和程序的患者,并且已经完成了至少两次给定的程序。我尝试过另一个自连接然后修改SELECT PatientID, Min(ProcedureDate) FROM ClinicalData cd1 INNER JOIN ClinicalData cd2 ON cd1.PatientID = cd2.PatientID AND cd2.Diagnosis IN ('Laceration', 'Lung-Cancer','Diabetes') INNER JOIN ClinicalData cd3 ON cd1.PatientID = cd3.PatientID WHERE cd1.Procedure IN ('Stitches', 'Cast', 'Hemoglobin-A1C-Check') AND cd3.Procedure IN ('Stitches', 'Cast', 'Hemoglobin-A1C-Check') GROUP BY PatientID 子句来搜索过程的第二个实例但是当我尝试运行查询时只是继续......然后......我必须有一些东西错误。这是我试过的SQL。

WHERE Procedure IN (SELECT Procedure From ProcedureList)

我试图拼凑一个简短的例子。在现实生活中,诊断和过程表示为代码,并且在查询中不进行硬编码。例如,它们通过查找表完成。 {{1}}

4 个答案:

答案 0 :(得分:2)

为什么不尝试这样的事情,它应该是你想要的:

SELECT patientid, MIN(proceduredate)
FROM @clinicaldata
WHERE diagnosis IN ('laceration', 'lung-cancer', 'diabetes') AND procedure IN ('stitches',      'cast', 'hemoglobin-a1c-check')
GROUP BY patientid
HAVING COUNT(*) > 1

即使你需要加入其他表,也不需要自己加入像这样的查询。它应该足以使用必须计算患者访问次数的次数,然后确保在where子句中指定您想要包含哪些诊断和程序就足够了。

答案 1 :(得分:2)

SELECT 
   PatientID,
   Min(ProcedureDate) 
FROM 
   ClinicalData cd1
WHERE
   [Procedure] IN ('Stitches', 'Cast', 'Hemoglobin-A1C-Check')
         AND Diagnosis IN ('Laceration', 'Lung-Cancer','Diabetes')
GROUP BY
   PatientID, Diagnosis, [Procedure]
Having Count(*) > 1

答案 2 :(得分:2)

假设SQL Server 2005+,您可以这样做:

;WITH CTE AS
(
    SELECT  PatientID,
            [Procedure],
            MIN(ProcedureDate) ProcedureDate
    FROM ClinicalData cd1
    WHERE NOT EXISTS(SELECT 1 FROM ClinicalData
                     WHERE Diagnosis NOT IN ('Laceration', 'Lung-Cancer','Diabetes')
                     AND PatientID = cd1.PatientID)
    GROUP BY PatientID,
             [Procedure]
    HAVING COUNT(*) >= 2
)
SELECT  PatientID,
        MIN(ProcedureDate) ProcedureDate
FROM CTE
GROUP BY PatientID

结果是:

╔═══════════╦════════════════╗
║ PATIENTID ║ PROCEDUREDATE  ║
╠═══════════╬════════════════╣
║         3 ║ March, 12 2013 ║
╚═══════════╩════════════════╝

here is a sqlfiddle一起演示。

答案 3 :(得分:2)

你可以尝试这样的事情:

SELECT 
   PatientID,
   Min(ProcedureDate),
   Distinct(Procedure),
   Count(Diagnosis) as DiagnosisTotal
FROM 
   ClinicalData cd1
      INNER JOIN ClinicalData cd2 ON cd1.PatientID = PatientID
         AND Diagnosis IN ('Laceration', 'Lung-Cancer','Diabetes')
WHERE
   Procedure IN ('Stitches', 'Cast', 'Hemoglobin-A1C-Check')
       AND DiagnosisTotal > 1
GROUP BY
   PatientID