SQL Server:如何获取最新的日期和值

时间:2014-11-10 20:34:10

标签: sql sql-server

我正在尝试为每行获取最新的CreationTimeValue,但我为每个条件获取了多行。有人可以帮忙吗?

这是我的代码和结果:

谢谢

SELECT DISTINCT
       a.PatientAccountID
       ,e.FindingAbbr
       ,e.Value
       ,e.CreationTime
FROM 
    PatientVisit a with (nolock)
INNER JOIN 
    Assessment d with (nolock) ON a.PatientVisit_oid = d.PatientVisit_oid
INNER JOIN 
    Observation e with (nolock) ON e.AssessmentID = d.AssessmentID
WHERE
    a.PatientAccountID = '11'

     AND a.VisitTypeCode='IP'
     AND a.VisitEndDateTime is null
     AND e.Value != ''

    AND e.FindingAbbr IN ('A_IV1 Site', 'A_IV2 Site', 'A_IV3 Site', 'A_IV4 Site')

     AND EXISTS
    (SELECT DISTINCT Max(e.CreationTime)
     FROM Observation e with (nolock))
     Order by a.PatientAccountID

我的结果如下:

        PatientAccountID    FindingAbbr Value   CreationTime
11  A_IV1 Site  L Forearm            11/10/2014 9:47
11  A_IV1 Site  L Forearm            11/10/2014 1:40
11  A_IV1 Site  L Forearm            11/9/2014 18:18
11  A_IV4 Site  L Forearm            10/31/2014 20:57
11  A_IV4 Site  L Forearm            10/31/2014 10:26
11  A_IV4 Site  L Forearm            10/30/2014 22:31
11  A_IV4 Site  L Forearm            10/29/2014 23:10
11  A_IV4 Site  L Forearm            10/29/2014 8:31
11  A_IV4 Site  R Antecubital   11/10/2014 9:47
11  A_IV4 Site  R Antecubital   11/10/2014 1:40
11  A_IV4 Site  R Antecubital   11/9/2014 18:18
11  A_IV4 Site  R Antecubital   11/9/2014 18:17
11  A_IV4 Site  R Antecubital   11/9/2014 11:12
11  A_IV2 Site  R Antecubital   10/31/2014 10:26
11  A_IV2 Site  R Antecubital   10/30/2014 22:31
11  A_IV2 Site  R Antecubital   10/29/2014 23:10
11  A_IV2 Site  R Antecubital   10/29/2014 8:31
11  A_IV3 Site  R Forearm            11/10/2014 9:47
11  A_IV3 Site  R Forearm            11/10/2014 1:40
11  A_IV3 Site  R Forearm            11/9/2014 18:18
11  A_IV3 Site  R Forearm            11/9/2014 18:17
11  A_IV3 Site  R Forearm            11/9/2014 11:12
11  A_IV3 Site  R Forearm            11/8/2014 20:57
11  A_IV3 Site  R Forearm            11/8/2014 7:40
11  A_IV3 Site  R Forearm            11/8/2014 4:24
11  A_IV1 Site  R Forearm            10/31/2014 20:57
11  A_IV1 Site  R Forearm            10/31/2014 10:26
11  A_IV3 Site  R Upper Arm 10/31/2014 20:57
11  A_IV3 Site  R Upper Arm 10/31/2014 10:26
11  A_IV3 Site  R Upper Arm 10/30/2014 22:31
11  A_IV3 Site  R Upper Arm 10/29/2014 23:10
11  A_IV3 Site  R Upper Arm 10/29/2014 8:31
11  A_IV3 Site  R Upper Arm 10/28/2014 23:52
11  A_IV3 Site  R Upper Arm 10/28/2014 13:41
11  A_IV3 Site  R Upper Arm 10/28/2014 9:26

我希望我的结果如下:

PatientAccountID   FindingAbbr   Value                CreationTime
----------------------------------------------------------------------
    11             A_IV1 Site      L Forearm          11/10/2014 9:47
    11             A_IV2 Site      R Antecubital      10/31/2014 10:26
    11             A_IV3 Site      R Forearm          11/10/2014 9:47
    11             A_IV4 Site      R Antecubital      11/10/2014 9:47

大家好: 谢谢大家的帮助。我尝试了你们的所有输入,它确实消除了大部分重复的行。但是现在我为每个FindingAbbr获得了2行,我理解它是因为与它相关的唯一值,但是我有什么方法可以根据最新的CreationTime为每个FindingAbbr只获得一行。以下是我现在得到的结果。 谢谢。

PatientAccountID    FindingAbbr Value   CreationTime
11  A_IV1 Site  L Forearm   11/10/2014 9:47
11  A_IV1 Site  R Forearm   10/31/2014 20:57
11  A_IV2 Site  R Forearm   11/10/2014 9:47
11  A_IV2 Site  R Antecubital   10/31/2014 20:57
11  A_IV3 Site  R Forearm   11/10/2014 9:47
11  A_IV3 Site  R Upper Arm 10/31/2014 20:57
11  A_IV4 Site  R Antecubital   11/10/2014 9:47
11  A_IV4 Site  L Forearm   10/31/2014 20:57

5 个答案:

答案 0 :(得分:2)

使用窗口功能:

SELECT PatientAccountID, FindingAbbr, Value, CreationTime
FROM (SELECT a.PatientAccountID, e.FindingAbbr, e.Value, e.CreationTime,
             ROW_NUMBER() OVER (PARTITION BY e.FindingAbbr ORDER BY e.CreationTime) as seqnum,
             COUNT(*) OVER (PARTITION BY e.Value) as cnt
      FROM PatientVisit a with (nolock) INNER JOIN
           Assessment d with (nolock)
           ON a.PatientVisit_oid = d.PatientVisit_oid INNER JOIN
           Observation e with (nolock)
           ON e.AssessmentID = d.AssessmentID
      WHERE a.PatientAccountID = '11' AND
            a.VisitTypeCode='IP' AND
            a.VisitEndDateTime is null AND
            e.Value <> '' AND
            e.FindingAbbr IN ('A_IV1 Site', 'A_IV2 Site', 'A_IV3 Site', 'A_IV4 Site')
     ) t
WHERE seqnum = 1 or seqnum = cnt;

答案 1 :(得分:0)

尝试在FindingAbbr,Value和PatientID上执行GROUP BY,然后在CreationTime上放置MAX(),以便它知道选择最新条目

SELECT    a.PatientAccountID ,e.FindingAbbr,
          e.Value, max(e.CreationTime) as CreationTime
FROM      dbo.vw_ORE_PatientVisitInfo a with (nolock)
JOIN      dbo.hAssessment d with (nolock) ON a.PatientVisit_oid = d.PatientVisit_oid
JOIN      dbo.HObservation e with (nolock) ON e.AssessmentID = d.AssessmentID
WHERE     a.PatientAccountID= '11'
          AND a.VisitTypeCode='IP'
          AND a.VisitEndDateTime is null
          AND e.Value <> ''
          AND e.FindingAbbr IN ('A_IV1 Site', 'A_IV2 Site', 'A_IV3 Site', 'A_IV4 Site')
          AND EXISTS
         (SELECT DISTINCT Max(e.CreationTime)
         FROM dbo.HObservation e with (nolock))
         Order by a.PatientAccountID)
GROUP BY    e.FindingAbbr, e.PatientAccountID, e.Value

答案 2 :(得分:0)

一般的想法是做这样的事情。你可以弄清楚细节。

select yourfields
from table1
join (
select somefield, max(DateTimeField) maxdt
from table1 
group by somefield
where whatever) temp  on table1.somefield = temp.somefield
and DateTimeField = maxdt
where whatever

答案 3 :(得分:0)

我认为他想要这个:

SELECT    e.PatientAccountID ,e.FindingAbbr,
          e.Value, max(e.CreationTime) as CreationTime
FROM      dbo.vw_ORE_PatientVisitInfo a with (nolock)
JOIN      dbo.hAssessment d with (nolock) ON a.PatientVisit_oid = d.PatientVisit_oid
JOIN      dbo.HObservation e with (nolock) ON e.AssessmentID = d.AssessmentID
WHERE     a.PatientAccountID= '11'
          AND a.VisitTypeCode='IP'
          AND a.VisitEndDateTime is null
          AND e.Value != ''
          AND e.FindingAbbr IN ('A_IV1 Site', 'A_IV2 Site', 'A_IV3 Site', 'A_IV4 Site')
          AND EXISTS
         (SELECT DISTINCT Max(e.CreationTime)
         FROM dbo.HObservation e with (nolock))
         Order by a.PatientAccountID)
GROUP BY    e.FindingAbbr, e.PatientAccountID, e.Value

答案 4 :(得分:0)

像这样的东西

select PatientAccountID,FindingAbbr,Value,LatestCreationTime from
(SELECT
   a.PatientAccountID
   ,e.FindingAbbr
   ,e.Value
   ,max(e.CreationTime) as LatestCreationTime


FROM PatientVisit a with (nolock)
INNER JOIN Assessment d with (nolock)
ON a.PatientVisit_oid = d.PatientVisit_oid
INNER JOIN Observation e with (nolock)
ON e.AssessmentID = d.AssessmentID
WHERE
a.PatientAccountID= '11'

 AND a.VisitTypeCode='IP'
 AND a.VisitEndDateTime is null
 AND e.Value != ''

AND e.FindingAbbr IN ('A_IV1 Site', 'A_IV2 Site', 'A_IV3 Site', 'A_IV4 Site')
 Group BY a.PatientAccountID
   ,e.FindingAbbr) as query
Order by a.PatientAccountID