如何获取最小值和最新日期[Sql Server 2008]
How to get MIN value and latest date [Sql Server 2008]
我可以获取最新日期,但无法获取最小值。我想在这里得到的是我想为每个患者显示每个 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
为什么不将您的最小值和最大创建时间分开,为什么不在您的 ROW_NUMBER() 窗口函数中将它们结合起来
ROW_NUMBER() OVER (PARTITION BY a.AccountID, e.FindingAbbr
ORDER BY e.Value, e.CreationTime DESC)
ROW_NUMBER() 1 将是该特定 accountID 和 FindingAbbr 的最低值的最新创建时间。
你很接近。删除 cte 中 Value
列上的 min
,将 e.Value
添加到 group by
,并将 value
添加到您的 row_number order by RN
列。这应该为您提供每个 findingAbbr 的最小值,以及最近的日期。
WITH cteRankedData As
(
SELECT DISTINCT
a.AccountID
,e.FindingAbbr
,e.Value as Value
,e.CreationTime
,ROW_NUMBER() OVER (PARTITION BY a.AccountID, e.FindingAbbr ORDER BY e.Value ASC, 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, e.Value
)
SELECT
AccountID
,FindingAbbr
,Value
,CreationTime
FROM
cteRankedData
WHERE
RN = 1
ORDER BY
AccountID
,CreationTime
;
我可以获取最新日期,但无法获取最小值。我想在这里得到的是我想为每个患者显示每个 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
为什么不将您的最小值和最大创建时间分开,为什么不在您的 ROW_NUMBER() 窗口函数中将它们结合起来
ROW_NUMBER() OVER (PARTITION BY a.AccountID, e.FindingAbbr
ORDER BY e.Value, e.CreationTime DESC)
ROW_NUMBER() 1 将是该特定 accountID 和 FindingAbbr 的最低值的最新创建时间。
你很接近。删除 cte 中 Value
列上的 min
,将 e.Value
添加到 group by
,并将 value
添加到您的 row_number order by RN
列。这应该为您提供每个 findingAbbr 的最小值,以及最近的日期。
WITH cteRankedData As
(
SELECT DISTINCT
a.AccountID
,e.FindingAbbr
,e.Value as Value
,e.CreationTime
,ROW_NUMBER() OVER (PARTITION BY a.AccountID, e.FindingAbbr ORDER BY e.Value ASC, 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, e.Value
)
SELECT
AccountID
,FindingAbbr
,Value
,CreationTime
FROM
cteRankedData
WHERE
RN = 1
ORDER BY
AccountID
,CreationTime
;