如何在左连接中进行GROUP BY

时间:2012-05-05 07:24:32

标签: sql-server database

*

我得到的结果如下

PID在此重复,2行显示1名患者(约翰)

我必须为patient_name(约翰)只显示一行 如果他在同一天或任何其他日期有多次访问,我必须为患者约翰显示一行..

6 个答案:

答案 0 :(得分:6)

每位患者显示一行 - 以及他/她的上次访问日期:

SELECT 
    Patient_Master.PID, 
    Patient_Master.Patient_ID,
    Patient_Master.Patient_Name,
    Patient_Master.Sex,
    Patient_Master.Patients_Birth_Date,
    Patient_Last_Visit.Visit_Date 
FROM 
    Patient_Master 
  LEFT JOIN
    ( SELECT 
          PID,
          MAX(Visit_Date) AS Visit_Date
      FROM
          Patient_Visit
      GROUP BY
          PID 
    ) AS Patient_Last_Visit
        ON Patient_Master.PID = Patient_Last_Visit.PID 
ORDER BY 
    Patient_Master.Patient_Name

答案 1 :(得分:1)

@nikhil你能更具体一点吗?

如果您想为患者提供一行,为什么要加入餐桌?

我认为您应该对Patient_Master中的列进行分组,并对Patient_Visit进行一些汇总

编辑: 这应该工作

    SELECT 
        Patient_Master.PID, 
        Patient_Master.Patient_ID,
        Patient_Master.Patient_Name,
        Patient_Master.Sex,
        Patient_Master.Patients_Birth_Date
    FROM 
        Patient_Master 
    INNER JOIN
        Patient_Visit ON Patient_Master.PID = Patient_Visit.PID 
    GROUP BY
        Patient_Master.PID, 
        Patient_Master.Patient_ID,
        Patient_Master.Patient_Name,
        Patient_Master.Sex,
        Patient_Master.Patients_Birth_Date
    ORDER BY 
        Patient_Master.Patient_Name

此查询选择至少一次访问的患者信息。

答案 2 :(得分:1)

使用下面的查询将返回单行..如果行的所有值都相同

SELECT 
    Patient_Master.PID, 
    Patient_Master.Patient_ID,
    Patient_Master.Patient_Name,
    Patient_Master.Sex,
    Patient_Master.Patients_Birth_Date,
    Patient_Visit.Visit_Date 
FROM 
    Patient_Master 
LEFT JOIN
    Patient_Visit ON Patient_Master.PID = Patient_Visit.PID    
Group by 
    Patient_Master.PID, 
    Patient_Master.Patient_ID,
    Patient_Master.Patient_Name,
    Patient_Master.Sex,
    Patient_Master.Patients_Birth_Date,
    Patient_Visit.Visit_Date   
ORDER BY 
    Patient_Master.Patient_Name

答案 3 :(得分:1)

我认为这会给你单行,试试这个

SELECT 
        Patient_Master.PID, 
        Patient_Master.Patient_ID,
        Patient_Master.Patient_Name,
        Patient_Master.Sex,
        Patient_Master.Patients_Birth_Date,
        Patient_Visit.Visit_Date 
    FROM 
        Patient_Master 
    INNER JOIN
        Patient_Visit ON Patient_Master.PID = Patient_Visit.PID 
    GROUP BY Patient_Master.PID
    ORDER BY 
        Patient_Master.Patient_Name

答案 4 :(得分:1)

每个不同的日期显示一名患者:

SELECT  M.PID, 
        M.Patient_ID,
        M.Patient_Name,
        M.Sex,
        M.Patients_Birth_Date,
        A.Visit_Date 
FROM    Patient_Master AS M 
        OUTER APPLY
        (
                SELECT  DISTINCT V.Visit_Date
                FROM    Patient_Visit AS V
                WHERE   V.PID = M.Pid
        ) AS A;

显示一名患者和最近的日期:

SELECT  M.PID, 
        M.Patient_ID,
        M.Patient_Name,
        M.Sex,
        M.Patients_Birth_Date,
        A.Visit_Date 
FROM    Patient_Master AS M 
        OUTER APPLY
        (
            SELECT  MAX(V.Visit_Date) AS Visit_Date
            FROM    Patient_Visit AS V
            WHERE   V.PID = M.Pid
        ) AS A;

答案 5 :(得分:0)

我不明白你需要什么。也许

GROUP BY PID, Patient_ID, Patient_Name, Sex,
         Patients_Birth_Date,MAX(Visit_Trans_ID),MAX(Visit_Date)