English 中文(简体)
如何按左派群体分类
原标题:How to GROUP BY in left join

* E/CN.6/2009/1。

页: 1

<>PID在此重复,有两行显示有1名病人(john)

I have to display only 1 row for patient_name (john) i have to display one row for patient john if he have multiple visits on the same date or any other date..

最佳回答

每一病人一行——及其最后一次访问日期:

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
问题回答

use below query that will return single row..if all the values for rows are same

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

我认为,这将给你一行,尝试这样做。

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

@nikhil 你们是否更具体?

If you want to have just one row for patient why are you joining tables?

我认为,你应该把病人的栏目——Master的栏目和病人的一些总体——分类起来。 访 问

EDIT: This should work

    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

该询问选择了至少一次访问的病人的信息。

Show one patient per distinct date:

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;

我不理解你们需要什么。 Maybe

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




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签