选择具有相似疾病名称ef core 3.1的患者

mrfwxfqh  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(363)

我有下列表格
患者

ID   PatientName   PatientIDNumber  
 ---- ------------- ----------------- 
   1   Jo                  411420607  
   2   Mark                206047758

记录

ID    DiseaseName    PatinetID  
 ---- --------------- ----------- 
   1   Liver Disease           1  
   2   Heart Disease           1  
   3   Liver Disease           2  
   4   Heart Disease           2

每个病人都有很多记录
患者数据模型类

public class PatientEntity : BaseEntity
     {
         [Column("PatientName")]
         [StringLength(150)]
         public string Name { get; set; }

         [Column("OfficialIDNumber")]
         [StringLength(10), MinLength(10)]
         public string IDNumber { get; set; }

         [Column("SystemIDnumber")]
         public int SystemID { get; set; }

         [Column("PatientDateOfBirth")]
         public DateTime? DateOfBirth { get; set; }

         [Column("PatientEmailAdress")]
         [StringLength(300)]
         public string EmailAdress { get; set; }

         public IEnumerable<LookupsEntity> MetaData { get; set; }

         public IEnumerable<RecordEntity> Records { get; set; }
     }

记录数据模型类

public class RecordEntity : BaseEntity
 {
     [StringLength(50)]
     public string DiseaseName { get; set; }

     public DateTime TimeOfEntry { get; set; }

     [StringLength(300)]
     public string Description { get; set; }

     [ForeignKey("Patient")]
     public int PatientId { get; set; }
     public PatientEntity Patient { get; set; }

     public BillEntity Bill { get; set; }

 }

我想创建一个api,它将一个特定的病人id作为一个参数,以获取具有类似疾病的其他病人的列表
患者报告api结果(dto)

public class PatinetReportResource
 {
     public PatientResource patient { get; set; }
     public IList<PatientResource> SimilarPatinets { get; set; }
 }

注:类似疾病是指两位患者有2种或2种以上的共同疾病
如何在ef core 3.1中列出患有类似疾病的患者?

bq8i3lrv

bq8i3lrv1#

这是你所需要的,而不是依赖于你的疾病名称在同一顺序的赤裸裸的骨骼。
如果你需要其他信息,比如 PatientName ,您可以通过 Package select 在下面 cte 然后 join 在患者数据上:
作为小提琴。

declare @p table(ID int,PatientName varchar(10),PatientIDNumber int);
insert into @p values
 (1,'Jo',411420607)
,(2,'Mark',206047758)
,(3,'Paul',552065834)
,(4,'Lisa',653025132);

declare @r table(ID int,DiseaseName varchar(30),PatientID int);
insert into @r values
 (1,'Liver Disease',1)
,(2,'Heart Disease',1)
,(3,'Liver Disease',2)
,(4,'Heart Disease',2)
,(5,'Liver Disease',3)
,(6,'Lung Disease',3)
,(7,'Arm Disease',4)
,(8,'Lung Disease',4)
,(9,'Liver Disease',4)
,(10,'Heart Disease',4);

declare @id int = 1;

select r.PatientID
        ,r2.PatientID as MatchedPatientID
from @r as r
    join @r as r2
        on r.DiseaseName = r2.DiseaseName
            and r.PatientID <> r2.PatientID
where r.PatientID = @id
group by r.PatientID
        ,r2.PatientID
having count(r2.PatientID) >= 2;
sd2nnvve

sd2nnvve2#

这是我对最初问题的解释。
最简单的方法是字符串聚合:

select diseases, string_agg(patientid, ',') within group (order by patientid) as patients
from (select patientId, string_agg(diseasename, ',') within group (order by diseasename) as diseases
      from t
      group by patientid
     ) p
group by diseases;

这是一把小提琴。

相关问题