我正在尝试从观察表获取最新数据。为了确保我获得最新值,我在值的“创建日期/时间”上使用max函数。为了将我的结果缩小到特定患者,我必须将此表加入患者表。 我很想发出一张桌子的视觉效果,但不能,因为这是我的第一篇文章。无论如何,看看你是否可以在没有桌面设计的情况下搞清楚。
我执行了以下操作:
select o.FindingName, o.Value, o.CreationTime
from dbo.HObservation o inner join dbo.HPatient p
on p.ObjectID = o.Patient_oid,
(select max(CreationTime) as CT, value
from dbo.HObservation group by Value ) b
Where o.Value = b.Value and
o.CreationTime = b.CT
and o.FindingAbbr in ( 'Wt', 'A_BM', 'A_Last BM Date', 'A_BP',
'A_Pulse', 'A_Temperature', 'A_Respirations',
'A_R_SurgDate', 'A_R_ChRvORTm', 'A_R_OpPOPPreced'
, 'A_R_FNSNN', 'A_R_WOUNDNN')
and p.ObjectID = 2227268
我的结果似乎显示上表中列出的所有数据。 我尝试了多种场景,但我的大脑现在有点油腻,我甚至不记得它们。有谁知道我在这里做错了什么? 另外,您可能已经注意到我试图将“查找名称”列表缩小到仅12个值。只是为了让它更有效但是没有帮助。 任何想法!! ??
答案 0 :(得分:0)
HObservation表是否会为每位患者提供记录/值字段。
在您的查询上
(select max(CreationTime) as CT, value from dbo.HObservation group by Value )
您只按价值获取最长时间。如果您的餐桌有多名患者,那么它将仅列出最近更新的患者的最大值。
您可以包含Patient_oid列以及值以获取max(creationTime)
答案 1 :(得分:0)
您从问题中留下了一些细节:
此查询应该为您提供一般概念:
with WindowedObservation as (
select Patient_oid, FindingAbbr, Value, CreationTime,
row_number() over (
partition by Patient_oid, FindingAbbr
order by CreationTime desc
) as ROWNUM
from HObservation
)
select *
from WindowedObservation
join HPatient
on HPatient.ObjectId = WindowedObservation.Patient_oid
where WindowedObservation.ROWNUM = 1
注释掉WHERE子句以了解如何使用ROW_NUMBER()。
以下是需要测试的少量样本数据:
CREATE TABLE HPatient (
ObjectId int,
Name nvarchar(50)
)
insert into HPatient(ObjectId, Name)
select 1,'Patient1'
union all select 2, 'Patient2'
union all select 3,'Patient3'
CREATE TABLE HObservation (
Patient_oid int,
FindingAbbr nvarchar(50),
Value float,
CreationTime datetime
)
insert into HObservation(Patient_oid, FindingAbbr, Value, CreationTime)
select 1, 'A_Pulse', 64, '2012-01-01'
union all select 1, 'A_Pulse', 73, '2012-01-02'
union all select 1, 'A_Pulse', 59, '2012-01-03'
union all select 1, 'A_Temperature', 98.6, '2012-01-01'
union all select 1, 'A_Temperature', 98.1, '2012-01-02'
union all select 1, 'A_Temperature', 90.2, '2012-01-03'
union all select 2, 'A_Pulse', 61, '2012-01-03'
union all select 2, 'A_Pulse', 67, '2012-01-04'
union all select 2, 'A_Pulse', 64, '2012-01-05'
union all select 2, 'A_Temperature', 100.2, '2012-01-03'
union all select 2, 'A_Temperature', 98.6, '2012-01-04'
union all select 2, 'A_Temperature', 98.9, '2012-01-05'
union all select 3, 'A_Pulse', 80, '2012-02-01'
union all select 3, 'A_Temperature', 98.2, '2012-02-01'
union all select 3, 'Wt', 180, '2012-02-01'
union all select 3, 'A_Pulse', 84, '2012-02-02'
union all select 3, 'A_Respirations', 16, '2012-02-03'