根据最新日期获取记录

时间:2015-01-31 13:38:51

标签: sql sql-server sql-server-2012

在下面的查询中,我只需要一个记录,这是最新的phDate。

 WITH TEMP_PAT_PIP_AddedBy
AS (
    SELECT S.ppId
        ,(
            SELECT PersonID
            FROM [PFMADestination].[dbo].PERSON
            WHERE SourceType = 'user_Users'
                AND SourceId = S.uuId
            ) AS AddedByToStore
        ,phDate
    FROM [PFMADestination].[dbo].PatientPIP D
    INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
        AND phEvent = 'PIP-info uppdaterad'
    )
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId

CTE的预期输出仅为每个ppId获取最新的phDate,如果超过1个记录则忽略其他,然后更新它。

enter image description here

2 个答案:

答案 0 :(得分:1)

使用partition by子句可以获得预期的输出。请尝试以下查询:

WITH TEMP_PAT_PIP_AddedBy
AS (
    SELECT S.ppId
        ,(
            SELECT PersonID
            FROM [PFMADestination].[dbo].PERSON
            WHERE SourceType = 'user_Users'
                AND SourceId = S.uuId
            ) AS AddedByToStore
        ,phDate , ROW_NUMBER() OVER(PARTITION BY S.ppId ORDER BY phDate DESC) AS NUM
    FROM [PFMADestination].[dbo].PatientPIP D
    INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
        AND phEvent = 'PIP-info uppdaterad'
    )

UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P 
ON T.ppId = P.PatientId AND T.NUM=1

答案 1 :(得分:1)

将phDate = max phDate条件添加到where子句:

WITH TEMP_PAT_PIP_AddedBy
AS (
    SELECT S.ppId
        ,(
            SELECT PersonID
            FROM [PFMADestination].[dbo].PERSON
            WHERE SourceType = 'user_Users'
                AND SourceId = S.uuId
            ) AS AddedByToStore
        ,phDate
    FROM [PFMADestination].[dbo].PatientPIP D
    INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
        AND phEvent = 'PIP-info uppdaterad'
    )
SELECT *
FROM TEMP_PAT_PIP_AddedBy
where phDate = (select max(phDate) from TEMP_PAT_PIP_AddedBy
                where ppId = TEMP_PAT_PIP_AddedBy.ppId) 
ORDER BY ppId
    ,phDate DESC

编辑版,phDate是P列吗? (如果没有,请编辑我的答案。)

 WITH TEMP_PAT_PIP_AddedBy
    AS (
        SELECT S.ppId
            ,(
                SELECT PersonID
                FROM [PFMADestination].[dbo].PERSON
                WHERE SourceType = 'user_Users'
                    AND SourceId = S.uuId
                ) AS AddedByToStore
            ,phDate
        FROM [PFMADestination].[dbo].PatientPIP D
        INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
            AND phEvent = 'PIP-info uppdaterad'
            AND phDate = (select max(phDate) from P
                          where ppId = P.ppId)
        )
    --select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
    UPDATE P
    SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
    FROM TEMP_PAT_PIP_AddedBy T
    INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId

斯文?