我有一张表MyTable
,其中的属性为:
Id(PK)
patientId(int)
Transfusion_Date(datetime)
checkup_date(datetime)
血红蛋白_水平(浮动)
对于每个checkup_date
,都有一个唯一的haemoglobin_level
,
一个patientId
可以包含多个Transfusion_Date
以及多个checkup_date
(此处checkup_date
表示衡量haemoglobin_level
的日期)
我需要检索记录,以便在haemoglobin_level
patientId
的{{1}}(s)
请帮我完成我必须写的查询。
答案 0 :(得分:0)
这将为myPatId
的{{1}}提供一条或多条记录,该记录位于您指定的checkup_date
之前。
myTransfusionDate
答案 1 :(得分:0)
declare @PatientId int;
declare @MyTable table (
Id int primary key,
patientId int,
Transfusion_Date datetime,
checkup_date datetime,
haemoglobin_level float
);
select *
from @MyTable t
where t.patientId = @PatientId
and t.checkup_date <= t.Transfusion_Date
and not exists (
select 0 from @MyTable t2
where t2.patientId = t.patientId
and t2.checkup_date between t.checkup_date and t.Transfusion_Date
);