我能够获得最新日期,但无法获得最小值。我想在这里得到的是我希望为每位患者显示每个findingAbbr
的最小值及其最新创建日期和最小值的时间。有人可以告诉我这个问题我做错了吗?
感谢您的帮助!!!
WITH cteRankedData As
(
SELECT DISTINCT
a.AccountID
,e.FindingAbbr
,min(e.Value) as Value
,e.CreationTime
,ROW_NUMBER() OVER (PARTITION BY a.AccountID, e.FindingAbbr ORDER BY e.CreationTime DESC) As RN
FROM dbo.PatientVisitInfo a with (nolock)
JOIN dbo.Assessment d with (nolock) ON a.PatientVisit_oid = d.PatientVisit_oid
JOIN dbo.Observation e with (nolock) ON e.AssessmentID = d.AssessmentID
WHERE
a.VisitTypeCode='IP'
AND a.VisitEndDateTime is null
AND e.Value <> ''
AND e.FindingAbbr IN ('A_PEEPSet','A_KR_RT_FI02%','A_FIO2%Set','A_FIO2%', 'A_Vent Mode')
AND e.CreationTime >= DATEADD(d,-1, GETDATE())
group by a.AccountID ,e.FindingAbbr, e.CreationTime)
SELECT
AccountID
,FindingAbbr
,Value
,CreationTime
FROM
cteRankedData
WHERE
RN = 1
ORDER BY
AccountID
,CreationTime
;
结果我得到了:
AccountID FindingAbbr Value CreationTime
1 _FIO2% 40 1/7/15 1:55 PM
2 A_FIO2% 60 1/7/15 8:20 AM
2 A_FIO2%Set 60 1/7/15 9:47 AM
2 A_PEEPSet 8 1/7/15 9:47 AM
2 A_Vent Mode CMV 1/7/15 9:47 AM
3 A_FIO2%Set 70 1/7/15 7:21 AM
4 A_KR_RT_FI02% 30 1/6/15 2:54 PM
4 A_FIO2% 30 1/7/15 9:35 AM
4 A_FIO2%Set 45 1/7/15 10:22 AM
4 A_PEEPSet 5 1/7/15 10:22 AM
4 A_Vent Mode CMV 1/7/15 10:22 AM
所有结果:
AccountID FindingAbbr Value CreationTime
1 A_FIO2% 40 1/7/15 1:55 PM
2 A_FIO2% 60 1/7/15 8:20 AM
2 A_FIO2% 60 1/7/15 8:20 AM
2 A_FIO2% 100 1/7/15 1:31 AM
2 A_FIO2% 100 1/7/15 3:30 AM
2 A_FIO2% Other:70 1/7/15 4:11 AM
2 A_FIO2%Set 60 1/7/15 3:49 AM
2 A_FIO2%Set 60 1/7/15 9:45 AM
2 A_FIO2%Set 60 1/7/15 9:47 AM
2 A_FIO2%Set 100 1/7/15 1:29 AM
2 A_PEEPSet 8 1/7/15 1:29 AM
2 A_PEEPSet 8 1/7/15 9:47 AM
2 A_PEEPSet 5 1/7/15 9:45 AM
2 A_PEEPSet 8 1/7/15 3:49 AM
2 A_Vent Mode CMV 1/7/15 3:49 AM
2 A_Vent Mode CMV 1/7/15 9:45 AM
2 A_Vent Mode CMV 1/7/15 9:47 AM
2 A_Vent Mode CMV 1/7/15 1:29 AM
3 A_FIO2%Set 70 1/6/15 3:09 PM
3 A_FIO2%Set 70 1/7/15 7:21 AM
4 A_FIO2% 30 1/7/15 4:26 AM
4 A_FIO2% 30 1/7/15 9:35 AM
4 A_FIO2%Set 45 1/7/15 9:53 AM
4 A_FIO2%Set 45 1/7/15 10:22 AM
4 A_FIO2%Set 45 1/7/15 3:55 AM
4 A_FIO2%Set 45 1/6/15 7:22 PM
4 A_FIO2%Set 45 1/6/15 11:02 PM
4 A_KR_RT_FI02% 30 1/6/15 2:54 PM
4 A_PEEPSet 5 1/6/15 7:22 PM
4 A_PEEPSet 5 1/6/15 11:02 PM
4 A_PEEPSet 5 1/7/15 3:55 AM
4 A_PEEPSet 5 1/7/15 10:22 AM
4 A_PEEPSet 5 1/7/15 9:53 AM
4 A_Vent Mode CMV 1/7/15 9:53 AM
4 A_Vent Mode CMV 1/7/15 10:22 AM
4 A_Vent Mode CMV 1/7/15 3:55 AM
4 A_Vent Mode CMV 1/6/15 11:02 PM
4 A_Vent Mode CMV 1/6/15 7:22 PM
4 A_Vent Mode CPAP 1/6/15 2:54 PM
我想要的结果:
AccountID FindingAbbr Value CreationTime
1 A_FIO2% 40 1/7/15 1:55 PM
2 A_FIO2% 60 1/7/15 8:20 AM
2 A_FIO2%Set 60 1/7/15 9:47 AM
2 A_PEEPSet 5 1/7/15 9:45 AM
2 A_Vent Mode CMV 1/7/15 9:47 AM
3 A_FIO2%Set 70 1/7/15 7:21 AM
4 A_KR_RT_FI02% 30 1/6/15 2:54 PM
4 A_FIO2% 30 1/7/15 9:35 AM
4 A_FIO2%Set 45 1/7/15 10:22 AM
4 A_PEEPSet 5 1/7/15 10:22 AM
4 A_Vent Mode CMV 1/7/15 10:22 AM
答案 0 :(得分:0)
为什么不在你的ROW_NUMBER()窗口函数中将它们组合在一起,而不是分离你的最小值和最大创建时间
ROW_NUMBER() OVER (PARTITION BY a.AccountID, e.FindingAbbr
ORDER BY e.Value, e.CreationTime DESC)
ROW_NUMBER()1将是该特定accountID和FindingAbbr的最低值的最新创建时间。
答案 1 :(得分:0)
你很亲密。移除cte中min
列的Value
,将e.Value
添加到group by
,然后将value
添加到您的row_number订单中{{1} }列。这应该为您提供每个findsAbbr的最小值,以及最近的日期。
RN